blob: 66fcb263cc59b11a5d2463ec10f189c5b27261a4 [file] [log] [blame]
Janis Danisevskis1af91262020-08-10 14:58:08 -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
Janis Danisevskis1af91262020-08-10 14:58:08 -070015//! This crate implements the IKeystoreSecurityLevel interface.
16
Janis Danisevskis2ee014b2021-05-05 14:29:08 -070017use crate::attestation_key_utils::{get_attest_key_info, AttestationKeyInfo};
Pavel Grafovf45034a2021-05-12 22:35:45 +010018use crate::audit_log::{
19 log_key_deleted, log_key_generated, log_key_imported, log_key_integrity_violation,
20};
Janis Danisevskisf84d0b02022-01-26 14:11:14 -080021use crate::database::{BlobInfo, CertificateInfo, KeyIdGuard};
Janis Danisevskis2ee014b2021-05-05 14:29:08 -070022use crate::error::{self, map_km_error, map_or_log_err, Error, ErrorCode};
Janis Danisevskis0ffb8a82022-02-06 22:37:21 -080023use crate::globals::{DB, ENFORCEMENTS, LEGACY_IMPORTER, SUPER_KEY};
Janis Danisevskis2ee014b2021-05-05 14:29:08 -070024use crate::key_parameter::KeyParameter as KsKeyParam;
25use crate::key_parameter::KeyParameterValue as KsKeyParamValue;
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +000026use crate::ks_err;
Hasini Gunasinghe15891e62021-06-10 16:23:27 +000027use crate::metrics_store::log_key_creation_event_stats;
Janis Danisevskis2ee014b2021-05-05 14:29:08 -070028use crate::remote_provisioning::RemProvState;
29use crate::super_key::{KeyBlob, SuperKeyManager};
30use crate::utils::{
Seth Moore66d9e902022-03-16 17:20:31 -070031 check_device_attestation_permissions, check_key_permission,
32 check_unique_id_attestation_permissions, is_device_id_attestation_tag,
Janis Danisevskis5f3a0572021-06-18 11:26:42 -070033 key_characteristics_to_internal, uid_to_android_user, watchdog as wd,
Janis Danisevskis2ee014b2021-05-05 14:29:08 -070034};
35use crate::{
36 database::{
37 BlobMetaData, BlobMetaEntry, DateTime, KeyEntry, KeyEntryLoadBits, KeyMetaData,
38 KeyMetaEntry, KeyType, SubComponentType, Uuid,
39 },
40 operation::KeystoreOperation,
41 operation::LoggingInfo,
42 operation::OperationDb,
43 permission::KeyPerm,
44};
Janis Danisevskis5cb52dc2021-04-07 16:31:18 -070045use crate::{globals::get_keymint_device, id_rotation::IdRotationState};
Shawn Willden708744a2020-12-11 13:05:27 +000046use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{
Janis Danisevskis3541f3e2021-03-20 14:18:52 -070047 Algorithm::Algorithm, AttestationKey::AttestationKey,
Shawn Willden8fde4c22021-02-14 13:58:22 -070048 HardwareAuthenticatorType::HardwareAuthenticatorType, IKeyMintDevice::IKeyMintDevice,
49 KeyCreationResult::KeyCreationResult, KeyFormat::KeyFormat,
Max Bires8e93d2b2021-01-14 13:17:59 -080050 KeyMintHardwareInfo::KeyMintHardwareInfo, KeyParameter::KeyParameter,
51 KeyParameterValue::KeyParameterValue, SecurityLevel::SecurityLevel, Tag::Tag,
Janis Danisevskis1af91262020-08-10 14:58:08 -070052};
Andrew Walbrande45c8b2021-04-13 14:42:38 +000053use android_hardware_security_keymint::binder::{BinderFeatures, Strong, ThreadState};
Janis Danisevskis1af91262020-08-10 14:58:08 -070054use android_system_keystore2::aidl::android::system::keystore2::{
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -070055 AuthenticatorSpec::AuthenticatorSpec, CreateOperationResponse::CreateOperationResponse,
Janis Danisevskisb2434d02021-04-20 12:49:27 -070056 Domain::Domain, EphemeralStorageKeyResponse::EphemeralStorageKeyResponse,
57 IKeystoreOperation::IKeystoreOperation, IKeystoreSecurityLevel::BnKeystoreSecurityLevel,
Janis Danisevskis1af91262020-08-10 14:58:08 -070058 IKeystoreSecurityLevel::IKeystoreSecurityLevel, KeyDescriptor::KeyDescriptor,
Janis Danisevskisd43c1b92021-11-09 14:56:17 +000059 KeyMetadata::KeyMetadata, KeyParameters::KeyParameters, ResponseCode::ResponseCode,
Janis Danisevskis1af91262020-08-10 14:58:08 -070060};
Janis Danisevskis212c68b2021-01-14 22:29:28 -080061use anyhow::{anyhow, Context, Result};
Janis Danisevskisd43c1b92021-11-09 14:56:17 +000062use std::convert::TryInto;
63use std::time::SystemTime;
Janis Danisevskis1af91262020-08-10 14:58:08 -070064
65/// Implementation of the IKeystoreSecurityLevel Interface.
66pub struct KeystoreSecurityLevel {
67 security_level: SecurityLevel,
Janis Danisevskis5f3a0572021-06-18 11:26:42 -070068 keymint: Strong<dyn IKeyMintDevice>,
Max Bires8e93d2b2021-01-14 13:17:59 -080069 hw_info: KeyMintHardwareInfo,
70 km_uuid: Uuid,
Janis Danisevskis1af91262020-08-10 14:58:08 -070071 operation_db: OperationDb,
Max Bires97f96812021-02-23 23:44:57 -080072 rem_prov_state: RemProvState,
Janis Danisevskis5cb52dc2021-04-07 16:31:18 -070073 id_rotation_state: IdRotationState,
Janis Danisevskis1af91262020-08-10 14:58:08 -070074}
75
Janis Danisevskis1af91262020-08-10 14:58:08 -070076// Blob of 32 zeroes used as empty masking key.
77static ZERO_BLOB_32: &[u8] = &[0; 32];
78
Janis Danisevskis2c084012021-01-31 22:23:17 -080079// Per RFC 5280 4.1.2.5, an undefined expiration (not-after) field should be set to GeneralizedTime
80// 999912312359559, which is 253402300799000 ms from Jan 1, 1970.
81const UNDEFINED_NOT_AFTER: i64 = 253402300799000i64;
82
Janis Danisevskis1af91262020-08-10 14:58:08 -070083impl KeystoreSecurityLevel {
84 /// Creates a new security level instance wrapped in a
Andrew Walbrande45c8b2021-04-13 14:42:38 +000085 /// BnKeystoreSecurityLevel proxy object. It also enables
86 /// `BinderFeatures::set_requesting_sid` on the new interface, because
Janis Danisevskis1af91262020-08-10 14:58:08 -070087 /// we need it for checking keystore permissions.
88 pub fn new_native_binder(
89 security_level: SecurityLevel,
Janis Danisevskis5cb52dc2021-04-07 16:31:18 -070090 id_rotation_state: IdRotationState,
Stephen Crane221bbb52020-12-16 15:52:10 -080091 ) -> Result<(Strong<dyn IKeystoreSecurityLevel>, Uuid)> {
Max Bires8e93d2b2021-01-14 13:17:59 -080092 let (dev, hw_info, km_uuid) = get_keymint_device(&security_level)
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +000093 .context(ks_err!("KeystoreSecurityLevel::new_native_binder."))?;
Andrew Walbrande45c8b2021-04-13 14:42:38 +000094 let result = BnKeystoreSecurityLevel::new_binder(
95 Self {
96 security_level,
97 keymint: dev,
98 hw_info,
99 km_uuid,
100 operation_db: OperationDb::new(),
101 rem_prov_state: RemProvState::new(security_level, km_uuid),
102 id_rotation_state,
103 },
104 BinderFeatures { set_requesting_sid: true, ..BinderFeatures::default() },
105 );
Max Bires8e93d2b2021-01-14 13:17:59 -0800106 Ok((result, km_uuid))
Janis Danisevskis1af91262020-08-10 14:58:08 -0700107 }
108
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700109 fn watch_millis(&self, id: &'static str, millis: u64) -> Option<wd::WatchPoint> {
110 let sec_level = self.security_level;
111 wd::watch_millis_with(id, millis, move || format!("SecurityLevel {:?}", sec_level))
112 }
113
Janis Danisevskis1af91262020-08-10 14:58:08 -0700114 fn store_new_key(
115 &self,
116 key: KeyDescriptor,
Shawn Willdendbdac602021-01-12 22:35:16 -0700117 creation_result: KeyCreationResult,
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000118 user_id: u32,
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000119 flags: Option<i32>,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700120 ) -> Result<KeyMetadata> {
Shawn Willdendbdac602021-01-12 22:35:16 -0700121 let KeyCreationResult {
122 keyBlob: key_blob,
123 keyCharacteristics: key_characteristics,
124 certificateChain: mut certificate_chain,
125 } = creation_result;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700126
Max Bires8e93d2b2021-01-14 13:17:59 -0800127 let mut cert_info: CertificateInfo = CertificateInfo::new(
Shawn Willdendbdac602021-01-12 22:35:16 -0700128 match certificate_chain.len() {
129 0 => None,
130 _ => Some(certificate_chain.remove(0).encodedCertificate),
131 },
132 match certificate_chain.len() {
133 0 => None,
134 _ => Some(
135 certificate_chain
136 .iter()
Chariseea1e1c482022-02-26 01:26:35 +0000137 .flat_map(|c| c.encodedCertificate.iter())
Shawn Willdendbdac602021-01-12 22:35:16 -0700138 .copied()
139 .collect(),
140 ),
141 },
142 );
143
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000144 let mut key_parameters = key_characteristics_to_internal(key_characteristics);
145
146 key_parameters.push(KsKeyParam::new(
147 KsKeyParamValue::UserID(user_id as i32),
148 SecurityLevel::SOFTWARE,
149 ));
Janis Danisevskis04b02832020-10-26 09:21:40 -0700150
Shaquille Johnsonaec2eca2022-11-30 17:08:05 +0000151 let creation_date = DateTime::now().context(ks_err!("Trying to make creation time."))?;
Janis Danisevskisb42fc182020-12-15 08:41:27 -0800152
Janis Danisevskis1af91262020-08-10 14:58:08 -0700153 let key = match key.domain {
Satya Tangirala60671e32021-03-04 16:12:19 -0800154 Domain::BLOB => KeyDescriptor {
155 domain: Domain::BLOB,
156 blob: Some(key_blob.to_vec()),
157 ..Default::default()
158 },
Janis Danisevskis1af91262020-08-10 14:58:08 -0700159 _ => DB
Janis Danisevskis93927dd2020-12-23 12:23:08 -0800160 .with::<_, Result<KeyDescriptor>>(|db| {
Satya Tangirala60671e32021-03-04 16:12:19 -0800161 let mut db = db.borrow_mut();
162
163 let (key_blob, mut blob_metadata) = SUPER_KEY
Janis Danisevskis0fd25a62022-01-04 19:53:37 -0800164 .read()
165 .unwrap()
Satya Tangirala60671e32021-03-04 16:12:19 -0800166 .handle_super_encryption_on_key_init(
167 &mut db,
Janis Danisevskis0ffb8a82022-02-06 22:37:21 -0800168 &LEGACY_IMPORTER,
Satya Tangirala60671e32021-03-04 16:12:19 -0800169 &(key.domain),
170 &key_parameters,
171 flags,
172 user_id,
173 &key_blob,
174 )
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000175 .context(ks_err!("Failed to handle super encryption."))?;
Satya Tangirala60671e32021-03-04 16:12:19 -0800176
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800177 let mut key_metadata = KeyMetaData::new();
178 key_metadata.add(KeyMetaEntry::CreationDate(creation_date));
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800179 blob_metadata.add(BlobMetaEntry::KmUuid(self.km_uuid));
Janis Danisevskis93927dd2020-12-23 12:23:08 -0800180
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800181 let key_id = db
Janis Danisevskis93927dd2020-12-23 12:23:08 -0800182 .store_new_key(
Janis Danisevskis66784c42021-01-27 08:40:25 -0800183 &key,
Janis Danisevskis0cabd712021-05-25 11:07:10 -0700184 KeyType::Client,
Janis Danisevskis93927dd2020-12-23 12:23:08 -0800185 &key_parameters,
Janis Danisevskisf84d0b02022-01-26 14:11:14 -0800186 &BlobInfo::new(&key_blob, &blob_metadata),
Max Bires8e93d2b2021-01-14 13:17:59 -0800187 &cert_info,
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800188 &key_metadata,
Max Bires8e93d2b2021-01-14 13:17:59 -0800189 &self.km_uuid,
Janis Danisevskis93927dd2020-12-23 12:23:08 -0800190 )
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000191 .context(ks_err!())?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700192 Ok(KeyDescriptor {
193 domain: Domain::KEY_ID,
Janis Danisevskisaec14592020-11-12 09:41:49 -0800194 nspace: key_id.id(),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700195 ..Default::default()
196 })
197 })
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000198 .context(ks_err!())?,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700199 };
200
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700201 Ok(KeyMetadata {
202 key,
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700203 keySecurityLevel: self.security_level,
Max Bires8e93d2b2021-01-14 13:17:59 -0800204 certificate: cert_info.take_cert(),
205 certificateChain: cert_info.take_cert_chain(),
Janis Danisevskis04b02832020-10-26 09:21:40 -0700206 authorizations: crate::utils::key_parameters_to_authorizations(key_parameters),
Janis Danisevskisb42fc182020-12-15 08:41:27 -0800207 modificationTimeMs: creation_date.to_millis_epoch(),
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700208 })
Janis Danisevskis1af91262020-08-10 14:58:08 -0700209 }
210
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700211 fn create_operation(
Janis Danisevskis1af91262020-08-10 14:58:08 -0700212 &self,
213 key: &KeyDescriptor,
214 operation_parameters: &[KeyParameter],
215 forced: bool,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700216 ) -> Result<CreateOperationResponse> {
Janis Danisevskis1af91262020-08-10 14:58:08 -0700217 let caller_uid = ThreadState::get_calling_uid();
218 // We use `scoping_blob` to extend the life cycle of the blob loaded from the database,
219 // so that we can use it by reference like the blob provided by the key descriptor.
220 // Otherwise, we would have to clone the blob from the key descriptor.
221 let scoping_blob: Vec<u8>;
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800222 let (km_blob, key_properties, key_id_guard, blob_metadata) = match key.domain {
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700223 Domain::BLOB => {
Janis Danisevskis39d57e72021-10-19 16:56:20 -0700224 check_key_permission(KeyPerm::Use, key, &None)
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000225 .context(ks_err!("checking use permission for Domain::BLOB."))?;
Janis Danisevskis186d9f42021-03-03 14:40:52 -0800226 if forced {
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000227 check_key_permission(KeyPerm::ReqForcedOp, key, &None)
228 .context(ks_err!("checking forced permission for Domain::BLOB."))?;
Janis Danisevskis186d9f42021-03-03 14:40:52 -0800229 }
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700230 (
231 match &key.blob {
Janis Danisevskis1af91262020-08-10 14:58:08 -0700232 Some(blob) => blob,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700233 None => {
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000234 return Err(Error::sys()).context(ks_err!(
235 "Key blob must be specified when \
236 using Domain::BLOB."
237 ));
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700238 }
239 },
240 None,
Hasini Gunasinghe888dd352020-11-17 23:08:39 +0000241 None,
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000242 BlobMetaData::new(),
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700243 )
244 }
245 _ => {
Janis Danisevskisf84d0b02022-01-26 14:11:14 -0800246 let super_key = SUPER_KEY
247 .read()
248 .unwrap()
249 .get_per_boot_key_by_user_id(uid_to_android_user(caller_uid));
Janis Danisevskisaec14592020-11-12 09:41:49 -0800250 let (key_id_guard, mut key_entry) = DB
251 .with::<_, Result<(KeyIdGuard, KeyEntry)>>(|db| {
Janis Danisevskisf84d0b02022-01-26 14:11:14 -0800252 LEGACY_IMPORTER.with_try_import(key, caller_uid, super_key, || {
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +0000253 db.borrow_mut().load_key_entry(
Chris Wailesd5aaaef2021-07-27 16:04:33 -0700254 key,
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +0000255 KeyType::Client,
256 KeyEntryLoadBits::KM,
257 caller_uid,
Janis Danisevskis186d9f42021-03-03 14:40:52 -0800258 |k, av| {
Janis Danisevskis39d57e72021-10-19 16:56:20 -0700259 check_key_permission(KeyPerm::Use, k, &av)?;
Janis Danisevskis186d9f42021-03-03 14:40:52 -0800260 if forced {
Janis Danisevskis39d57e72021-10-19 16:56:20 -0700261 check_key_permission(KeyPerm::ReqForcedOp, k, &av)?;
Janis Danisevskis186d9f42021-03-03 14:40:52 -0800262 }
263 Ok(())
264 },
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +0000265 )
266 })
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700267 })
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000268 .context(ks_err!("Failed to load key blob."))?;
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800269
270 let (blob, blob_metadata) =
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000271 key_entry.take_key_blob_info().ok_or_else(Error::sys).context(ks_err!(
272 "Successfully loaded key entry, \
273 but KM blob was missing."
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800274 ))?;
275 scoping_blob = blob;
276
Qi Wub9433b52020-12-01 14:52:46 +0800277 (
278 &scoping_blob,
279 Some((key_id_guard.id(), key_entry.into_key_parameters())),
280 Some(key_id_guard),
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000281 blob_metadata,
Qi Wub9433b52020-12-01 14:52:46 +0800282 )
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700283 }
284 };
Janis Danisevskis1af91262020-08-10 14:58:08 -0700285
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700286 let purpose = operation_parameters.iter().find(|p| p.tag == Tag::PURPOSE).map_or(
Janis Danisevskis1af91262020-08-10 14:58:08 -0700287 Err(Error::Km(ErrorCode::INVALID_ARGUMENT))
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000288 .context(ks_err!("No operation purpose specified.")),
Janis Danisevskis398e6be2020-12-17 09:29:25 -0800289 |kp| match kp.value {
290 KeyParameterValue::KeyPurpose(p) => Ok(p),
291 _ => Err(Error::Km(ErrorCode::INVALID_ARGUMENT))
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000292 .context(ks_err!("Malformed KeyParameter.")),
Janis Danisevskis398e6be2020-12-17 09:29:25 -0800293 },
Janis Danisevskis1af91262020-08-10 14:58:08 -0700294 )?;
295
Satya Tangirala2642ff92021-04-15 01:57:00 -0700296 // Remove Tag::PURPOSE from the operation_parameters, since some keymaster devices return
297 // an error on begin() if Tag::PURPOSE is in the operation_parameters.
298 let op_params: Vec<KeyParameter> =
299 operation_parameters.iter().filter(|p| p.tag != Tag::PURPOSE).cloned().collect();
300 let operation_parameters = op_params.as_slice();
301
Janis Danisevskis5ed8c532021-01-11 14:19:42 -0800302 let (immediate_hat, mut auth_info) = ENFORCEMENTS
303 .authorize_create(
304 purpose,
Qi Wub9433b52020-12-01 14:52:46 +0800305 key_properties.as_ref(),
306 operation_parameters.as_ref(),
Janis Danisevskise3f7d202021-03-20 14:21:22 -0700307 self.hw_info.timestampTokenRequired,
Janis Danisevskis5ed8c532021-01-11 14:19:42 -0800308 )
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000309 .context(ks_err!())?;
Hasini Gunasinghe888dd352020-11-17 23:08:39 +0000310
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000311 let km_blob = SUPER_KEY
Janis Danisevskis0fd25a62022-01-04 19:53:37 -0800312 .read()
313 .unwrap()
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000314 .unwrap_key_if_required(&blob_metadata, km_blob)
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000315 .context(ks_err!("Failed to handle super encryption."))?;
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000316
Janis Danisevskisaec14592020-11-12 09:41:49 -0800317 let (begin_result, upgraded_blob) = self
318 .upgrade_keyblob_if_required_with(
Janis Danisevskis5f3a0572021-06-18 11:26:42 -0700319 &*self.keymint,
Janis Danisevskisaec14592020-11-12 09:41:49 -0800320 key_id_guard,
Paul Crowley7a658392021-03-18 17:08:20 -0700321 &km_blob,
Max Bires55620ff2022-02-11 13:34:15 -0800322 blob_metadata.km_uuid().copied(),
Chris Wailesd5aaaef2021-07-27 16:04:33 -0700323 operation_parameters,
Janis Danisevskisaec14592020-11-12 09:41:49 -0800324 |blob| loop {
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700325 match map_km_error({
326 let _wp = self.watch_millis(
327 "In KeystoreSecurityLevel::create_operation: calling begin",
328 500,
329 );
Janis Danisevskis5f3a0572021-06-18 11:26:42 -0700330 self.keymint.begin(
331 purpose,
332 blob,
Chris Wailesd5aaaef2021-07-27 16:04:33 -0700333 operation_parameters,
Janis Danisevskis5f3a0572021-06-18 11:26:42 -0700334 immediate_hat.as_ref(),
335 )
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700336 }) {
Janis Danisevskisaec14592020-11-12 09:41:49 -0800337 Err(Error::Km(ErrorCode::TOO_MANY_OPERATIONS)) => {
Janis Danisevskis186d9f42021-03-03 14:40:52 -0800338 self.operation_db.prune(caller_uid, forced)?;
Janis Danisevskisaec14592020-11-12 09:41:49 -0800339 continue;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700340 }
Pavel Grafovf45034a2021-05-12 22:35:45 +0100341 v @ Err(Error::Km(ErrorCode::INVALID_KEY_BLOB)) => {
342 if let Some((key_id, _)) = key_properties {
343 if let Ok(Some(key)) =
344 DB.with(|db| db.borrow_mut().load_key_descriptor(key_id))
345 {
346 log_key_integrity_violation(&key);
347 } else {
348 log::error!("Failed to load key descriptor for audit log");
349 }
350 }
351 return v;
352 }
Janis Danisevskisaec14592020-11-12 09:41:49 -0800353 v => return v,
354 }
355 },
356 )
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000357 .context(ks_err!("Failed to begin operation."))?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700358
Janis Danisevskis5ed8c532021-01-11 14:19:42 -0800359 let operation_challenge = auth_info.finalize_create_authorization(begin_result.challenge);
Hasini Gunasinghe888dd352020-11-17 23:08:39 +0000360
Hasini Gunasinghe0aba68a2021-03-19 00:43:52 +0000361 let op_params: Vec<KeyParameter> = operation_parameters.to_vec();
362
Janis Danisevskis1af91262020-08-10 14:58:08 -0700363 let operation = match begin_result.operation {
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700364 Some(km_op) => self.operation_db.create_operation(
365 km_op,
366 caller_uid,
367 auth_info,
368 forced,
369 LoggingInfo::new(self.security_level, purpose, op_params, upgraded_blob.is_some()),
370 ),
371 None => {
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000372 return Err(Error::sys()).context(ks_err!(
373 "Begin operation returned successfully, \
374 but did not return a valid operation."
375 ));
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700376 }
Janis Danisevskis1af91262020-08-10 14:58:08 -0700377 };
378
Stephen Crane23cf7242022-01-19 17:49:46 +0000379 let op_binder: binder::Strong<dyn IKeystoreOperation> =
Janis Danisevskis1af91262020-08-10 14:58:08 -0700380 KeystoreOperation::new_native_binder(operation)
381 .as_binder()
382 .into_interface()
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000383 .context(ks_err!("Failed to create IKeystoreOperation."))?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700384
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700385 Ok(CreateOperationResponse {
386 iOperation: Some(op_binder),
Hasini Gunasinghe888dd352020-11-17 23:08:39 +0000387 operationChallenge: operation_challenge,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700388 parameters: match begin_result.params.len() {
389 0 => None,
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700390 _ => Some(KeyParameters { keyParameter: begin_result.params }),
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700391 },
Satya Tangiralae2016a82021-03-05 09:28:30 -0800392 // An upgraded blob should only be returned if the caller has permission
393 // to use Domain::BLOB keys. If we got to this point, we already checked
394 // that the caller had that permission.
395 upgradedBlob: if key.domain == Domain::BLOB { upgraded_blob } else { None },
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700396 })
Janis Danisevskis1af91262020-08-10 14:58:08 -0700397 }
398
Janis Danisevskisd43c1b92021-11-09 14:56:17 +0000399 fn add_required_parameters(
Janis Danisevskis5cb52dc2021-04-07 16:31:18 -0700400 &self,
Janis Danisevskise766edc2021-02-06 12:16:26 -0800401 uid: u32,
402 params: &[KeyParameter],
403 key: &KeyDescriptor,
404 ) -> Result<Vec<KeyParameter>> {
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800405 let mut result = params.to_vec();
Janis Danisevskisd43c1b92021-11-09 14:56:17 +0000406
407 // Unconditionally add the CREATION_DATETIME tag and prevent callers from
408 // specifying it.
409 if params.iter().any(|kp| kp.tag == Tag::CREATION_DATETIME) {
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000410 return Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)).context(ks_err!(
411 "KeystoreSecurityLevel::add_required_parameters: \
412 Specifying Tag::CREATION_DATETIME is not allowed."
413 ));
Janis Danisevskisd43c1b92021-11-09 14:56:17 +0000414 }
415
Janis Danisevskis2b3c7232021-12-20 13:16:23 -0800416 // Add CREATION_DATETIME only if the backend version Keymint V1 (100) or newer.
417 if self.hw_info.versionNumber >= 100 {
418 result.push(KeyParameter {
419 tag: Tag::CREATION_DATETIME,
420 value: KeyParameterValue::DateTime(
421 SystemTime::now()
422 .duration_since(SystemTime::UNIX_EPOCH)
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000423 .context(ks_err!(
424 "KeystoreSecurityLevel::add_required_parameters: \
425 Failed to get epoch time."
426 ))?
Janis Danisevskis2b3c7232021-12-20 13:16:23 -0800427 .as_millis()
428 .try_into()
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000429 .context(ks_err!(
430 "KeystoreSecurityLevel::add_required_parameters: \
431 Failed to convert epoch time."
432 ))?,
Janis Danisevskis2b3c7232021-12-20 13:16:23 -0800433 ),
434 });
435 }
Janis Danisevskisd43c1b92021-11-09 14:56:17 +0000436
Janis Danisevskis2c084012021-01-31 22:23:17 -0800437 // If there is an attestation challenge we need to get an application id.
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800438 if params.iter().any(|kp| kp.tag == Tag::ATTESTATION_CHALLENGE) {
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700439 let aaid = {
440 let _wp = self.watch_millis(
Janis Danisevskisd43c1b92021-11-09 14:56:17 +0000441 "In KeystoreSecurityLevel::add_required_parameters calling: get_aaid",
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700442 500,
443 );
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000444 keystore2_aaid::get_aaid(uid)
445 .map_err(|e| anyhow!(ks_err!("get_aaid returned status {}.", e)))
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700446 }?;
447
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800448 result.push(KeyParameter {
449 tag: Tag::ATTESTATION_APPLICATION_ID,
450 value: KeyParameterValue::Blob(aaid),
451 });
452 }
Janis Danisevskis2c084012021-01-31 22:23:17 -0800453
Janis Danisevskise766edc2021-02-06 12:16:26 -0800454 if params.iter().any(|kp| kp.tag == Tag::INCLUDE_UNIQUE_ID) {
Seth Moore66d9e902022-03-16 17:20:31 -0700455 if check_key_permission(KeyPerm::GenUniqueId, key, &None).is_err()
456 && check_unique_id_attestation_permissions().is_err()
457 {
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000458 return Err(Error::perm()).context(ks_err!(
459 "Caller does not have the permission to generate a unique ID"
460 ));
Seth Moore66d9e902022-03-16 17:20:31 -0700461 }
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000462 if self
463 .id_rotation_state
464 .had_factory_reset_since_id_rotation()
465 .context(ks_err!("Call to had_factory_reset_since_id_rotation failed."))?
466 {
Janis Danisevskisd43c1b92021-11-09 14:56:17 +0000467 result.push(KeyParameter {
Janis Danisevskis5cb52dc2021-04-07 16:31:18 -0700468 tag: Tag::RESET_SINCE_ID_ROTATION,
469 value: KeyParameterValue::BoolValue(true),
470 })
471 }
Janis Danisevskise766edc2021-02-06 12:16:26 -0800472 }
473
Bram Bonné5d6c5102021-02-24 15:09:18 +0100474 // If the caller requests any device identifier attestation tag, check that they hold the
475 // correct Android permission.
476 if params.iter().any(|kp| is_device_id_attestation_tag(kp.tag)) {
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000477 check_device_attestation_permissions().context(ks_err!(
Bram Bonné5d6c5102021-02-24 15:09:18 +0100478 "Caller does not have the permission to attest device identifiers."
479 ))?;
480 }
481
Janis Danisevskis2c084012021-01-31 22:23:17 -0800482 // If we are generating/importing an asymmetric key, we need to make sure
483 // that NOT_BEFORE and NOT_AFTER are present.
484 match params.iter().find(|kp| kp.tag == Tag::ALGORITHM) {
485 Some(KeyParameter { tag: _, value: KeyParameterValue::Algorithm(Algorithm::RSA) })
486 | Some(KeyParameter { tag: _, value: KeyParameterValue::Algorithm(Algorithm::EC) }) => {
487 if !params.iter().any(|kp| kp.tag == Tag::CERTIFICATE_NOT_BEFORE) {
488 result.push(KeyParameter {
489 tag: Tag::CERTIFICATE_NOT_BEFORE,
490 value: KeyParameterValue::DateTime(0),
491 })
492 }
493 if !params.iter().any(|kp| kp.tag == Tag::CERTIFICATE_NOT_AFTER) {
494 result.push(KeyParameter {
495 tag: Tag::CERTIFICATE_NOT_AFTER,
496 value: KeyParameterValue::DateTime(UNDEFINED_NOT_AFTER),
497 })
498 }
499 }
500 _ => {}
501 }
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800502 Ok(result)
503 }
504
Janis Danisevskis1af91262020-08-10 14:58:08 -0700505 fn generate_key(
506 &self,
507 key: &KeyDescriptor,
Shawn Willden8fde4c22021-02-14 13:58:22 -0700508 attest_key_descriptor: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700509 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700510 flags: i32,
Paul Crowleyd5653e52021-03-25 09:46:31 -0700511 _entropy: &[u8],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700512 ) -> Result<KeyMetadata> {
Janis Danisevskis1af91262020-08-10 14:58:08 -0700513 if key.domain != Domain::BLOB && key.alias.is_none() {
514 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000515 .context(ks_err!("Alias must be specified"));
Janis Danisevskis1af91262020-08-10 14:58:08 -0700516 }
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000517 let caller_uid = ThreadState::get_calling_uid();
Janis Danisevskis1af91262020-08-10 14:58:08 -0700518
519 let key = match key.domain {
520 Domain::APP => KeyDescriptor {
521 domain: key.domain,
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000522 nspace: caller_uid as i64,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700523 alias: key.alias.clone(),
524 blob: None,
525 },
526 _ => key.clone(),
527 };
528
529 // generate_key requires the rebind permission.
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700530 // Must return on error for security reasons.
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000531 check_key_permission(KeyPerm::Rebind, &key, &None).context(ks_err!())?;
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700532
533 let attestation_key_info = match (key.domain, attest_key_descriptor) {
534 (Domain::BLOB, _) => None,
Satya Tangiralafdb9c762021-03-09 22:34:22 -0800535 _ => DB
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700536 .with(|db| {
537 get_attest_key_info(
Satya Tangiralafdb9c762021-03-09 22:34:22 -0800538 &key,
539 caller_uid,
540 attest_key_descriptor,
541 params,
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700542 &self.rem_prov_state,
Satya Tangiralafdb9c762021-03-09 22:34:22 -0800543 &mut db.borrow_mut(),
544 )
545 })
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000546 .context(ks_err!("Trying to get an attestation key"))?,
Satya Tangiralafdb9c762021-03-09 22:34:22 -0800547 };
Janis Danisevskis5cb52dc2021-04-07 16:31:18 -0700548 let params = self
Janis Danisevskisd43c1b92021-11-09 14:56:17 +0000549 .add_required_parameters(caller_uid, params, &key)
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000550 .context(ks_err!("Trying to get aaid."))?;
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800551
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700552 let creation_result = match attestation_key_info {
553 Some(AttestationKeyInfo::UserGenerated {
554 key_id_guard,
555 blob,
556 blob_metadata,
557 issuer_subject,
558 }) => self
559 .upgrade_keyblob_if_required_with(
Janis Danisevskis5f3a0572021-06-18 11:26:42 -0700560 &*self.keymint,
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700561 Some(key_id_guard),
Paul Crowley7a658392021-03-18 17:08:20 -0700562 &KeyBlob::Ref(&blob),
Max Bires55620ff2022-02-11 13:34:15 -0800563 blob_metadata.km_uuid().copied(),
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700564 &params,
565 |blob| {
566 let attest_key = Some(AttestationKey {
567 keyBlob: blob.to_vec(),
568 attestKeyParams: vec![],
569 issuerSubjectName: issuer_subject.clone(),
570 });
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700571 map_km_error({
572 let _wp = self.watch_millis(
573 concat!(
574 "In KeystoreSecurityLevel::generate_key (UserGenerated): ",
575 "calling generate_key."
576 ),
577 5000, // Generate can take a little longer.
578 );
Janis Danisevskis5f3a0572021-06-18 11:26:42 -0700579 self.keymint.generateKey(&params, attest_key.as_ref())
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700580 })
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700581 },
582 )
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000583 .context(ks_err!("Using user generated attestation key."))
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700584 .map(|(result, _)| result),
Max Bires55620ff2022-02-11 13:34:15 -0800585 Some(AttestationKeyInfo::RemoteProvisioned {
586 key_id_guard,
587 attestation_key,
588 attestation_certs,
589 }) => self
590 .upgrade_keyblob_if_required_with(
591 &*self.keymint,
592 Some(key_id_guard),
593 &KeyBlob::Ref(&attestation_key.keyBlob),
594 Some(self.rem_prov_state.get_uuid()),
595 &[],
596 |blob| {
597 map_km_error({
598 let _wp = self.watch_millis(
599 concat!(
600 "In KeystoreSecurityLevel::generate_key (RemoteProvisioned): ",
601 "calling generate_key.",
602 ),
603 5000, // Generate can take a little longer.
604 );
605 let dynamic_attest_key = Some(AttestationKey {
606 keyBlob: blob.to_vec(),
607 attestKeyParams: vec![],
608 issuerSubjectName: attestation_key.issuerSubjectName.clone(),
609 });
610 self.keymint.generateKey(&params, dynamic_attest_key.as_ref())
611 })
612 },
613 )
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700614 .context("While generating Key with remote provisioned attestation key.")
Max Bires55620ff2022-02-11 13:34:15 -0800615 .map(|(mut result, _)| {
616 result.certificateChain.push(attestation_certs);
617 result
618 }),
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700619 None => map_km_error({
620 let _wp = self.watch_millis(
621 concat!(
622 "In KeystoreSecurityLevel::generate_key (No attestation): ",
623 "calling generate_key.",
624 ),
625 5000, // Generate can take a little longer.
626 );
Janis Danisevskis5f3a0572021-06-18 11:26:42 -0700627 self.keymint.generateKey(&params, None)
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700628 })
629 .context("While generating Key without explicit attestation key."),
Max Bires97f96812021-02-23 23:44:57 -0800630 }
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000631 .context(ks_err!())?;
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700632
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000633 let user_id = uid_to_android_user(caller_uid);
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000634 self.store_new_key(key, creation_result, user_id, Some(flags)).context(ks_err!())
Janis Danisevskis1af91262020-08-10 14:58:08 -0700635 }
636
637 fn import_key(
638 &self,
639 key: &KeyDescriptor,
Paul Crowleyd5653e52021-03-25 09:46:31 -0700640 _attestation_key: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700641 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700642 flags: i32,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700643 key_data: &[u8],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700644 ) -> Result<KeyMetadata> {
Janis Danisevskis1af91262020-08-10 14:58:08 -0700645 if key.domain != Domain::BLOB && key.alias.is_none() {
646 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000647 .context(ks_err!("Alias must be specified"));
Janis Danisevskis1af91262020-08-10 14:58:08 -0700648 }
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000649 let caller_uid = ThreadState::get_calling_uid();
Janis Danisevskis1af91262020-08-10 14:58:08 -0700650
651 let key = match key.domain {
652 Domain::APP => KeyDescriptor {
653 domain: key.domain,
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000654 nspace: caller_uid as i64,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700655 alias: key.alias.clone(),
656 blob: None,
657 },
658 _ => key.clone(),
659 };
660
661 // import_key requires the rebind permission.
Janis Danisevskis39d57e72021-10-19 16:56:20 -0700662 check_key_permission(KeyPerm::Rebind, &key, &None).context("In import_key.")?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700663
Janis Danisevskis5cb52dc2021-04-07 16:31:18 -0700664 let params = self
Janis Danisevskisd43c1b92021-11-09 14:56:17 +0000665 .add_required_parameters(caller_uid, params, &key)
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000666 .context(ks_err!("Trying to get aaid."))?;
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800667
Janis Danisevskis1af91262020-08-10 14:58:08 -0700668 let format = params
669 .iter()
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700670 .find(|p| p.tag == Tag::ALGORITHM)
Janis Danisevskis1af91262020-08-10 14:58:08 -0700671 .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
672 .context("No KeyParameter 'Algorithm'.")
Janis Danisevskis398e6be2020-12-17 09:29:25 -0800673 .and_then(|p| match &p.value {
674 KeyParameterValue::Algorithm(Algorithm::AES)
675 | KeyParameterValue::Algorithm(Algorithm::HMAC)
676 | KeyParameterValue::Algorithm(Algorithm::TRIPLE_DES) => Ok(KeyFormat::RAW),
677 KeyParameterValue::Algorithm(Algorithm::RSA)
678 | KeyParameterValue::Algorithm(Algorithm::EC) => Ok(KeyFormat::PKCS8),
679 v => Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
Shaquille Johnsonaec2eca2022-11-30 17:08:05 +0000680 .context(ks_err!("Unknown Algorithm {:?}.", v)),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700681 })
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000682 .context(ks_err!())?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700683
Janis Danisevskis5f3a0572021-06-18 11:26:42 -0700684 let km_dev = &self.keymint;
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700685 let creation_result = map_km_error({
686 let _wp =
687 self.watch_millis("In KeystoreSecurityLevel::import_key: calling importKey.", 500);
688 km_dev.importKey(&params, format, key_data, None /* attestKey */)
689 })
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000690 .context(ks_err!("Trying to call importKey"))?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700691
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000692 let user_id = uid_to_android_user(caller_uid);
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000693 self.store_new_key(key, creation_result, user_id, Some(flags)).context(ks_err!())
Janis Danisevskis1af91262020-08-10 14:58:08 -0700694 }
695
696 fn import_wrapped_key(
697 &self,
698 key: &KeyDescriptor,
699 wrapping_key: &KeyDescriptor,
700 masking_key: Option<&[u8]>,
701 params: &[KeyParameter],
702 authenticators: &[AuthenticatorSpec],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700703 ) -> Result<KeyMetadata> {
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800704 let wrapped_data: &[u8] = match key {
705 KeyDescriptor { domain: Domain::APP, blob: Some(ref blob), alias: Some(_), .. }
706 | KeyDescriptor {
707 domain: Domain::SELINUX, blob: Some(ref blob), alias: Some(_), ..
708 } => blob,
709 _ => {
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000710 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)).context(ks_err!(
711 "Alias and blob must be specified and domain must be APP or SELINUX. {:?}",
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800712 key
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000713 ));
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800714 }
715 };
Janis Danisevskis1af91262020-08-10 14:58:08 -0700716
Janis Danisevskisaec14592020-11-12 09:41:49 -0800717 if wrapping_key.domain == Domain::BLOB {
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000718 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
719 .context(ks_err!("Import wrapped key not supported for self managed blobs."));
Janis Danisevskisaec14592020-11-12 09:41:49 -0800720 }
721
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000722 let caller_uid = ThreadState::get_calling_uid();
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000723 let user_id = uid_to_android_user(caller_uid);
724
Janis Danisevskis1af91262020-08-10 14:58:08 -0700725 let key = match key.domain {
726 Domain::APP => KeyDescriptor {
727 domain: key.domain,
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000728 nspace: caller_uid as i64,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700729 alias: key.alias.clone(),
730 blob: None,
731 },
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800732 Domain::SELINUX => KeyDescriptor {
733 domain: Domain::SELINUX,
734 nspace: key.nspace,
735 alias: key.alias.clone(),
736 blob: None,
737 },
738 _ => panic!("Unreachable."),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700739 };
740
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800741 // Import_wrapped_key requires the rebind permission for the new key.
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000742 check_key_permission(KeyPerm::Rebind, &key, &None).context(ks_err!())?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700743
Janis Danisevskisf84d0b02022-01-26 14:11:14 -0800744 let super_key = SUPER_KEY.read().unwrap().get_per_boot_key_by_user_id(user_id);
745
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000746 let (wrapping_key_id_guard, mut wrapping_key_entry) = DB
Janis Danisevskis1af91262020-08-10 14:58:08 -0700747 .with(|db| {
Janis Danisevskisf84d0b02022-01-26 14:11:14 -0800748 LEGACY_IMPORTER.with_try_import(&key, caller_uid, super_key, || {
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +0000749 db.borrow_mut().load_key_entry(
Chris Wailesd5aaaef2021-07-27 16:04:33 -0700750 wrapping_key,
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +0000751 KeyType::Client,
752 KeyEntryLoadBits::KM,
753 caller_uid,
Janis Danisevskis39d57e72021-10-19 16:56:20 -0700754 |k, av| check_key_permission(KeyPerm::Use, k, &av),
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +0000755 )
756 })
Janis Danisevskis1af91262020-08-10 14:58:08 -0700757 })
758 .context("Failed to load wrapping key.")?;
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000759
Shaquille Johnsonaec2eca2022-11-30 17:08:05 +0000760 let (wrapping_key_blob, wrapping_blob_metadata) =
761 wrapping_key_entry.take_key_blob_info().ok_or_else(error::Error::sys).context(
762 ks_err!("No km_blob after successfully loading key. This should never happen."),
763 )?;
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000764
Janis Danisevskis0fd25a62022-01-04 19:53:37 -0800765 let wrapping_key_blob = SUPER_KEY
766 .read()
767 .unwrap()
768 .unwrap_key_if_required(&wrapping_blob_metadata, &wrapping_key_blob)
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000769 .context(ks_err!("Failed to handle super encryption for wrapping key."))?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700770
Janis Danisevskis1af91262020-08-10 14:58:08 -0700771 // km_dev.importWrappedKey does not return a certificate chain.
772 // TODO Do we assume that all wrapped keys are symmetric?
773 // let certificate_chain: Vec<KmCertificate> = Default::default();
774
775 let pw_sid = authenticators
776 .iter()
777 .find_map(|a| match a.authenticatorType {
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700778 HardwareAuthenticatorType::PASSWORD => Some(a.authenticatorId),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700779 _ => None,
780 })
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800781 .unwrap_or(-1);
Janis Danisevskis1af91262020-08-10 14:58:08 -0700782
783 let fp_sid = authenticators
784 .iter()
785 .find_map(|a| match a.authenticatorType {
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700786 HardwareAuthenticatorType::FINGERPRINT => Some(a.authenticatorId),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700787 _ => None,
788 })
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800789 .unwrap_or(-1);
Janis Danisevskis1af91262020-08-10 14:58:08 -0700790
791 let masking_key = masking_key.unwrap_or(ZERO_BLOB_32);
792
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800793 let (creation_result, _) = self
794 .upgrade_keyblob_if_required_with(
Janis Danisevskis5f3a0572021-06-18 11:26:42 -0700795 &*self.keymint,
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800796 Some(wrapping_key_id_guard),
Paul Crowley7a658392021-03-18 17:08:20 -0700797 &wrapping_key_blob,
Max Bires55620ff2022-02-11 13:34:15 -0800798 wrapping_blob_metadata.km_uuid().copied(),
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800799 &[],
800 |wrapping_blob| {
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700801 let _wp = self.watch_millis(
802 "In KeystoreSecurityLevel::import_wrapped_key: calling importWrappedKey.",
803 500,
804 );
Janis Danisevskis5f3a0572021-06-18 11:26:42 -0700805 let creation_result = map_km_error(self.keymint.importWrappedKey(
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800806 wrapped_data,
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800807 wrapping_blob,
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800808 masking_key,
Chris Wailesd5aaaef2021-07-27 16:04:33 -0700809 params,
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800810 pw_sid,
811 fp_sid,
812 ))?;
813 Ok(creation_result)
814 },
815 )
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000816 .context(ks_err!())?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700817
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000818 self.store_new_key(key, creation_result, user_id, None)
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000819 .context(ks_err!("Trying to store the new key."))
Janis Danisevskis1af91262020-08-10 14:58:08 -0700820 }
Janis Danisevskisaec14592020-11-12 09:41:49 -0800821
Paul Crowley7a658392021-03-18 17:08:20 -0700822 fn store_upgraded_keyblob(
823 key_id_guard: KeyIdGuard,
Max Bires55620ff2022-02-11 13:34:15 -0800824 km_uuid: Option<Uuid>,
Paul Crowley7a658392021-03-18 17:08:20 -0700825 key_blob: &KeyBlob,
826 upgraded_blob: &[u8],
827 ) -> Result<()> {
828 let (upgraded_blob_to_be_stored, new_blob_metadata) =
Chris Wailesd5aaaef2021-07-27 16:04:33 -0700829 SuperKeyManager::reencrypt_if_required(key_blob, upgraded_blob)
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000830 .context(ks_err!("Failed to handle super encryption."))?;
Paul Crowley7a658392021-03-18 17:08:20 -0700831
Paul Crowley44c02da2021-04-08 17:04:43 +0000832 let mut new_blob_metadata = new_blob_metadata.unwrap_or_default();
Paul Crowley7a658392021-03-18 17:08:20 -0700833 if let Some(uuid) = km_uuid {
Max Bires55620ff2022-02-11 13:34:15 -0800834 new_blob_metadata.add(BlobMetaEntry::KmUuid(uuid));
Paul Crowley7a658392021-03-18 17:08:20 -0700835 }
836
837 DB.with(|db| {
838 let mut db = db.borrow_mut();
839 db.set_blob(
840 &key_id_guard,
841 SubComponentType::KEY_BLOB,
842 Some(&upgraded_blob_to_be_stored),
843 Some(&new_blob_metadata),
844 )
845 })
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000846 .context(ks_err!("Failed to insert upgraded blob into the database."))
Paul Crowley7a658392021-03-18 17:08:20 -0700847 }
848
Janis Danisevskisaec14592020-11-12 09:41:49 -0800849 fn upgrade_keyblob_if_required_with<T, F>(
850 &self,
851 km_dev: &dyn IKeyMintDevice,
Janis Danisevskisf84d0b02022-01-26 14:11:14 -0800852 mut key_id_guard: Option<KeyIdGuard>,
Paul Crowley7a658392021-03-18 17:08:20 -0700853 key_blob: &KeyBlob,
Max Bires55620ff2022-02-11 13:34:15 -0800854 km_uuid: Option<Uuid>,
Janis Danisevskisaec14592020-11-12 09:41:49 -0800855 params: &[KeyParameter],
856 f: F,
857 ) -> Result<(T, Option<Vec<u8>>)>
858 where
859 F: Fn(&[u8]) -> Result<T, Error>,
860 {
Janis Danisevskisf84d0b02022-01-26 14:11:14 -0800861 let (v, upgraded_blob) = crate::utils::upgrade_keyblob_if_required_with(
862 km_dev,
863 key_blob,
864 params,
865 f,
866 |upgraded_blob| {
867 if key_id_guard.is_some() {
868 // Unwrap cannot panic, because the is_some was true.
869 let kid = key_id_guard.take().unwrap();
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000870 Self::store_upgraded_keyblob(kid, km_uuid, key_blob, upgraded_blob)
871 .context(ks_err!("store_upgraded_keyblob failed"))
Janis Danisevskisf84d0b02022-01-26 14:11:14 -0800872 } else {
873 Ok(())
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000874 }
Janis Danisevskisf84d0b02022-01-26 14:11:14 -0800875 },
876 )
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000877 .context(ks_err!())?;
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000878
Janis Danisevskisf84d0b02022-01-26 14:11:14 -0800879 // If no upgrade was needed, use the opportunity to reencrypt the blob if required
880 // and if the a key_id_guard is held. Note: key_id_guard can only be Some if no
881 // upgrade was performed above and if one was given in the first place.
882 if key_blob.force_reencrypt() {
883 if let Some(kid) = key_id_guard {
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000884 Self::store_upgraded_keyblob(kid, km_uuid, key_blob, key_blob)
885 .context(ks_err!("store_upgraded_keyblob failed in forced reencrypt"))?;
Paul Crowley8d5b2532021-03-19 10:53:07 -0700886 }
Janis Danisevskisaec14592020-11-12 09:41:49 -0800887 }
Janis Danisevskisf84d0b02022-01-26 14:11:14 -0800888 Ok((v, upgraded_blob))
Janis Danisevskisaec14592020-11-12 09:41:49 -0800889 }
Satya Tangirala3361b612021-03-08 14:36:11 -0800890
Janis Danisevskisb2434d02021-04-20 12:49:27 -0700891 fn convert_storage_key_to_ephemeral(
892 &self,
893 storage_key: &KeyDescriptor,
894 ) -> Result<EphemeralStorageKeyResponse> {
Satya Tangirala3361b612021-03-08 14:36:11 -0800895 if storage_key.domain != Domain::BLOB {
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000896 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
897 .context(ks_err!("Key must be of Domain::BLOB"));
Satya Tangirala3361b612021-03-08 14:36:11 -0800898 }
899 let key_blob = storage_key
900 .blob
901 .as_ref()
902 .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000903 .context(ks_err!("No key blob specified"))?;
Satya Tangirala3361b612021-03-08 14:36:11 -0800904
905 // convert_storage_key_to_ephemeral requires the associated permission
Janis Danisevskis39d57e72021-10-19 16:56:20 -0700906 check_key_permission(KeyPerm::ConvertStorageKeyToEphemeral, storage_key, &None)
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000907 .context(ks_err!("Check permission"))?;
Satya Tangirala3361b612021-03-08 14:36:11 -0800908
Janis Danisevskis5f3a0572021-06-18 11:26:42 -0700909 let km_dev = &self.keymint;
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700910 match {
911 let _wp = self.watch_millis(
912 concat!(
913 "In IKeystoreSecurityLevel::convert_storage_key_to_ephemeral: ",
914 "calling convertStorageKeyToEphemeral (1)"
915 ),
916 500,
917 );
918 map_km_error(km_dev.convertStorageKeyToEphemeral(key_blob))
919 } {
Janis Danisevskisb2434d02021-04-20 12:49:27 -0700920 Ok(result) => {
921 Ok(EphemeralStorageKeyResponse { ephemeralKey: result, upgradedBlob: None })
922 }
923 Err(error::Error::Km(ErrorCode::KEY_REQUIRES_UPGRADE)) => {
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700924 let upgraded_blob = {
925 let _wp = self.watch_millis(
926 "In convert_storage_key_to_ephemeral: calling upgradeKey",
927 500,
928 );
929 map_km_error(km_dev.upgradeKey(key_blob, &[]))
930 }
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000931 .context(ks_err!("Failed to upgrade key blob."))?;
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700932 let ephemeral_key = {
933 let _wp = self.watch_millis(
934 "In convert_storage_key_to_ephemeral: calling convertStorageKeyToEphemeral (2)",
935 500,
936 );
Janis Danisevskis84af4d12021-07-22 17:39:15 -0700937 map_km_error(km_dev.convertStorageKeyToEphemeral(&upgraded_blob))
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700938 }
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000939 .context(ks_err!(
Janis Danisevskisb2434d02021-04-20 12:49:27 -0700940 "Failed to retrieve ephemeral key (after upgrade)."
941 ))?;
942 Ok(EphemeralStorageKeyResponse {
943 ephemeralKey: ephemeral_key,
944 upgradedBlob: Some(upgraded_blob),
945 })
946 }
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000947 Err(e) => Err(e).context(ks_err!("Failed to retrieve ephemeral key.")),
Janis Danisevskisb2434d02021-04-20 12:49:27 -0700948 }
Satya Tangirala3361b612021-03-08 14:36:11 -0800949 }
Satya Tangirala04bca0d2021-03-08 22:27:54 -0800950
951 fn delete_key(&self, key: &KeyDescriptor) -> Result<()> {
952 if key.domain != Domain::BLOB {
953 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000954 .context(ks_err!("delete_key: Key must be of Domain::BLOB"));
Satya Tangirala04bca0d2021-03-08 22:27:54 -0800955 }
956
957 let key_blob = key
958 .blob
959 .as_ref()
960 .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000961 .context(ks_err!("delete_key: No key blob specified"))?;
Satya Tangirala04bca0d2021-03-08 22:27:54 -0800962
Janis Danisevskis39d57e72021-10-19 16:56:20 -0700963 check_key_permission(KeyPerm::Delete, key, &None)
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000964 .context(ks_err!("delete_key: Checking delete permissions"))?;
Satya Tangirala04bca0d2021-03-08 22:27:54 -0800965
Janis Danisevskis5f3a0572021-06-18 11:26:42 -0700966 let km_dev = &self.keymint;
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700967 {
968 let _wp =
969 self.watch_millis("In KeystoreSecuritylevel::delete_key: calling deleteKey", 500);
Shaquille Johnson9da2e1c2022-09-19 12:39:01 +0000970 map_km_error(km_dev.deleteKey(key_blob)).context(ks_err!("keymint device deleteKey"))
Janis Danisevskis2ee014b2021-05-05 14:29:08 -0700971 }
Satya Tangirala04bca0d2021-03-08 22:27:54 -0800972 }
Janis Danisevskis1af91262020-08-10 14:58:08 -0700973}
974
975impl binder::Interface for KeystoreSecurityLevel {}
976
977impl IKeystoreSecurityLevel for KeystoreSecurityLevel {
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700978 fn createOperation(
Janis Danisevskis1af91262020-08-10 14:58:08 -0700979 &self,
980 key: &KeyDescriptor,
981 operation_parameters: &[KeyParameter],
982 forced: bool,
Stephen Crane23cf7242022-01-19 17:49:46 +0000983 ) -> binder::Result<CreateOperationResponse> {
Hasini Gunasinghe5a893e82021-05-05 14:32:32 +0000984 let _wp = self.watch_millis("IKeystoreSecurityLevel::createOperation", 500);
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700985 map_or_log_err(self.create_operation(key, operation_parameters, forced), Ok)
Janis Danisevskis1af91262020-08-10 14:58:08 -0700986 }
987 fn generateKey(
988 &self,
989 key: &KeyDescriptor,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700990 attestation_key: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700991 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700992 flags: i32,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700993 entropy: &[u8],
Stephen Crane23cf7242022-01-19 17:49:46 +0000994 ) -> binder::Result<KeyMetadata> {
Hasini Gunasinghe5a893e82021-05-05 14:32:32 +0000995 // Duration is set to 5 seconds, because generateKey - especially for RSA keys, takes more
996 // time than other operations
997 let _wp = self.watch_millis("IKeystoreSecurityLevel::generateKey", 5000);
Hasini Gunasingheb7142972021-02-20 03:11:27 +0000998 let result = self.generate_key(key, attestation_key, params, flags, entropy);
Hasini Gunasinghe9617fd92021-04-01 22:27:07 +0000999 log_key_creation_event_stats(self.security_level, params, &result);
Pavel Grafov94243c22021-04-21 18:03:11 +01001000 log_key_generated(key, ThreadState::get_calling_uid(), result.is_ok());
Hasini Gunasingheb7142972021-02-20 03:11:27 +00001001 map_or_log_err(result, Ok)
Janis Danisevskis1af91262020-08-10 14:58:08 -07001002 }
1003 fn importKey(
1004 &self,
1005 key: &KeyDescriptor,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -07001006 attestation_key: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -07001007 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -07001008 flags: i32,
Janis Danisevskis1af91262020-08-10 14:58:08 -07001009 key_data: &[u8],
Stephen Crane23cf7242022-01-19 17:49:46 +00001010 ) -> binder::Result<KeyMetadata> {
Hasini Gunasinghe5a893e82021-05-05 14:32:32 +00001011 let _wp = self.watch_millis("IKeystoreSecurityLevel::importKey", 500);
Hasini Gunasingheb7142972021-02-20 03:11:27 +00001012 let result = self.import_key(key, attestation_key, params, flags, key_data);
Hasini Gunasinghe9617fd92021-04-01 22:27:07 +00001013 log_key_creation_event_stats(self.security_level, params, &result);
Pavel Grafov94243c22021-04-21 18:03:11 +01001014 log_key_imported(key, ThreadState::get_calling_uid(), result.is_ok());
Hasini Gunasingheb7142972021-02-20 03:11:27 +00001015 map_or_log_err(result, Ok)
Janis Danisevskis1af91262020-08-10 14:58:08 -07001016 }
1017 fn importWrappedKey(
1018 &self,
1019 key: &KeyDescriptor,
1020 wrapping_key: &KeyDescriptor,
1021 masking_key: Option<&[u8]>,
1022 params: &[KeyParameter],
1023 authenticators: &[AuthenticatorSpec],
Stephen Crane23cf7242022-01-19 17:49:46 +00001024 ) -> binder::Result<KeyMetadata> {
Hasini Gunasinghe5a893e82021-05-05 14:32:32 +00001025 let _wp = self.watch_millis("IKeystoreSecurityLevel::importWrappedKey", 500);
Hasini Gunasingheb7142972021-02-20 03:11:27 +00001026 let result =
1027 self.import_wrapped_key(key, wrapping_key, masking_key, params, authenticators);
Hasini Gunasinghe9617fd92021-04-01 22:27:07 +00001028 log_key_creation_event_stats(self.security_level, params, &result);
Pavel Grafov94243c22021-04-21 18:03:11 +01001029 log_key_imported(key, ThreadState::get_calling_uid(), result.is_ok());
Hasini Gunasingheb7142972021-02-20 03:11:27 +00001030 map_or_log_err(result, Ok)
Janis Danisevskis1af91262020-08-10 14:58:08 -07001031 }
Satya Tangirala3361b612021-03-08 14:36:11 -08001032 fn convertStorageKeyToEphemeral(
1033 &self,
1034 storage_key: &KeyDescriptor,
Stephen Crane23cf7242022-01-19 17:49:46 +00001035 ) -> binder::Result<EphemeralStorageKeyResponse> {
Hasini Gunasinghe5a893e82021-05-05 14:32:32 +00001036 let _wp = self.watch_millis("IKeystoreSecurityLevel::convertStorageKeyToEphemeral", 500);
Satya Tangirala3361b612021-03-08 14:36:11 -08001037 map_or_log_err(self.convert_storage_key_to_ephemeral(storage_key), Ok)
1038 }
Stephen Crane23cf7242022-01-19 17:49:46 +00001039 fn deleteKey(&self, key: &KeyDescriptor) -> binder::Result<()> {
Hasini Gunasinghe5a893e82021-05-05 14:32:32 +00001040 let _wp = self.watch_millis("IKeystoreSecurityLevel::deleteKey", 500);
Pavel Grafov94243c22021-04-21 18:03:11 +01001041 let result = self.delete_key(key);
1042 log_key_deleted(key, ThreadState::get_calling_uid(), result.is_ok());
1043 map_or_log_err(result, Ok)
Satya Tangirala04bca0d2021-03-08 22:27:54 -08001044 }
Janis Danisevskis1af91262020-08-10 14:58:08 -07001045}