Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1 | // 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 Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 15 | //! This crate implements the IKeystoreSecurityLevel interface. |
| 16 | |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 17 | use crate::attestation_key_utils::{get_attest_key_info, AttestationKeyInfo}; |
Pavel Grafov | f45034a | 2021-05-12 22:35:45 +0100 | [diff] [blame] | 18 | use crate::audit_log::{ |
| 19 | log_key_deleted, log_key_generated, log_key_imported, log_key_integrity_violation, |
| 20 | }; |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 21 | use crate::database::{BlobInfo, CertificateInfo, KeyIdGuard}; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 22 | use crate::error::{self, map_km_error, map_or_log_err, Error, ErrorCode}; |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 23 | use crate::globals::{DB, ENFORCEMENTS, LEGACY_IMPORTER, SUPER_KEY}; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 24 | use crate::key_parameter::KeyParameter as KsKeyParam; |
| 25 | use crate::key_parameter::KeyParameterValue as KsKeyParamValue; |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 26 | use crate::ks_err; |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 27 | use crate::metrics_store::log_key_creation_event_stats; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 28 | use crate::remote_provisioning::RemProvState; |
| 29 | use crate::super_key::{KeyBlob, SuperKeyManager}; |
| 30 | use crate::utils::{ |
Seth Moore | 66d9e90 | 2022-03-16 17:20:31 -0700 | [diff] [blame] | 31 | check_device_attestation_permissions, check_key_permission, |
| 32 | check_unique_id_attestation_permissions, is_device_id_attestation_tag, |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 33 | key_characteristics_to_internal, uid_to_android_user, watchdog as wd, |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 34 | }; |
| 35 | use 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 Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 45 | use crate::{globals::get_keymint_device, id_rotation::IdRotationState}; |
Shawn Willden | 708744a | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 46 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 47 | Algorithm::Algorithm, AttestationKey::AttestationKey, |
Shawn Willden | 8fde4c2 | 2021-02-14 13:58:22 -0700 | [diff] [blame] | 48 | HardwareAuthenticatorType::HardwareAuthenticatorType, IKeyMintDevice::IKeyMintDevice, |
| 49 | KeyCreationResult::KeyCreationResult, KeyFormat::KeyFormat, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 50 | KeyMintHardwareInfo::KeyMintHardwareInfo, KeyParameter::KeyParameter, |
| 51 | KeyParameterValue::KeyParameterValue, SecurityLevel::SecurityLevel, Tag::Tag, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 52 | }; |
Andrew Walbran | de45c8b | 2021-04-13 14:42:38 +0000 | [diff] [blame] | 53 | use android_hardware_security_keymint::binder::{BinderFeatures, Strong, ThreadState}; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 54 | use android_system_keystore2::aidl::android::system::keystore2::{ |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 55 | AuthenticatorSpec::AuthenticatorSpec, CreateOperationResponse::CreateOperationResponse, |
Janis Danisevskis | b2434d0 | 2021-04-20 12:49:27 -0700 | [diff] [blame] | 56 | Domain::Domain, EphemeralStorageKeyResponse::EphemeralStorageKeyResponse, |
| 57 | IKeystoreOperation::IKeystoreOperation, IKeystoreSecurityLevel::BnKeystoreSecurityLevel, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 58 | IKeystoreSecurityLevel::IKeystoreSecurityLevel, KeyDescriptor::KeyDescriptor, |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 59 | KeyMetadata::KeyMetadata, KeyParameters::KeyParameters, ResponseCode::ResponseCode, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 60 | }; |
Janis Danisevskis | 212c68b | 2021-01-14 22:29:28 -0800 | [diff] [blame] | 61 | use anyhow::{anyhow, Context, Result}; |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 62 | use std::convert::TryInto; |
| 63 | use std::time::SystemTime; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 64 | |
| 65 | /// Implementation of the IKeystoreSecurityLevel Interface. |
| 66 | pub struct KeystoreSecurityLevel { |
| 67 | security_level: SecurityLevel, |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 68 | keymint: Strong<dyn IKeyMintDevice>, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 69 | hw_info: KeyMintHardwareInfo, |
| 70 | km_uuid: Uuid, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 71 | operation_db: OperationDb, |
Max Bires | 97f9681 | 2021-02-23 23:44:57 -0800 | [diff] [blame] | 72 | rem_prov_state: RemProvState, |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 73 | id_rotation_state: IdRotationState, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 76 | // Blob of 32 zeroes used as empty masking key. |
| 77 | static ZERO_BLOB_32: &[u8] = &[0; 32]; |
| 78 | |
Janis Danisevskis | 2c08401 | 2021-01-31 22:23:17 -0800 | [diff] [blame] | 79 | // 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. |
| 81 | const UNDEFINED_NOT_AFTER: i64 = 253402300799000i64; |
| 82 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 83 | impl KeystoreSecurityLevel { |
| 84 | /// Creates a new security level instance wrapped in a |
Andrew Walbran | de45c8b | 2021-04-13 14:42:38 +0000 | [diff] [blame] | 85 | /// BnKeystoreSecurityLevel proxy object. It also enables |
| 86 | /// `BinderFeatures::set_requesting_sid` on the new interface, because |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 87 | /// we need it for checking keystore permissions. |
| 88 | pub fn new_native_binder( |
| 89 | security_level: SecurityLevel, |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 90 | id_rotation_state: IdRotationState, |
Stephen Crane | 221bbb5 | 2020-12-16 15:52:10 -0800 | [diff] [blame] | 91 | ) -> Result<(Strong<dyn IKeystoreSecurityLevel>, Uuid)> { |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 92 | let (dev, hw_info, km_uuid) = get_keymint_device(&security_level) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 93 | .context(ks_err!("KeystoreSecurityLevel::new_native_binder."))?; |
Andrew Walbran | de45c8b | 2021-04-13 14:42:38 +0000 | [diff] [blame] | 94 | 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 Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 106 | Ok((result, km_uuid)) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 109 | 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 Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 114 | fn store_new_key( |
| 115 | &self, |
| 116 | key: KeyDescriptor, |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 117 | creation_result: KeyCreationResult, |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 118 | user_id: u32, |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 119 | flags: Option<i32>, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 120 | ) -> Result<KeyMetadata> { |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 121 | let KeyCreationResult { |
| 122 | keyBlob: key_blob, |
| 123 | keyCharacteristics: key_characteristics, |
| 124 | certificateChain: mut certificate_chain, |
| 125 | } = creation_result; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 126 | |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 127 | let mut cert_info: CertificateInfo = CertificateInfo::new( |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 128 | 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() |
Charisee | a1e1c48 | 2022-02-26 01:26:35 +0000 | [diff] [blame] | 137 | .flat_map(|c| c.encodedCertificate.iter()) |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 138 | .copied() |
| 139 | .collect(), |
| 140 | ), |
| 141 | }, |
| 142 | ); |
| 143 | |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 144 | 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 Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 150 | |
Shaquille Johnson | aec2eca | 2022-11-30 17:08:05 +0000 | [diff] [blame^] | 151 | let creation_date = DateTime::now().context(ks_err!("Trying to make creation time."))?; |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 152 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 153 | let key = match key.domain { |
Satya Tangirala | 60671e3 | 2021-03-04 16:12:19 -0800 | [diff] [blame] | 154 | Domain::BLOB => KeyDescriptor { |
| 155 | domain: Domain::BLOB, |
| 156 | blob: Some(key_blob.to_vec()), |
| 157 | ..Default::default() |
| 158 | }, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 159 | _ => DB |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 160 | .with::<_, Result<KeyDescriptor>>(|db| { |
Satya Tangirala | 60671e3 | 2021-03-04 16:12:19 -0800 | [diff] [blame] | 161 | let mut db = db.borrow_mut(); |
| 162 | |
| 163 | let (key_blob, mut blob_metadata) = SUPER_KEY |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 164 | .read() |
| 165 | .unwrap() |
Satya Tangirala | 60671e3 | 2021-03-04 16:12:19 -0800 | [diff] [blame] | 166 | .handle_super_encryption_on_key_init( |
| 167 | &mut db, |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 168 | &LEGACY_IMPORTER, |
Satya Tangirala | 60671e3 | 2021-03-04 16:12:19 -0800 | [diff] [blame] | 169 | &(key.domain), |
| 170 | &key_parameters, |
| 171 | flags, |
| 172 | user_id, |
| 173 | &key_blob, |
| 174 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 175 | .context(ks_err!("Failed to handle super encryption."))?; |
Satya Tangirala | 60671e3 | 2021-03-04 16:12:19 -0800 | [diff] [blame] | 176 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 177 | let mut key_metadata = KeyMetaData::new(); |
| 178 | key_metadata.add(KeyMetaEntry::CreationDate(creation_date)); |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 179 | blob_metadata.add(BlobMetaEntry::KmUuid(self.km_uuid)); |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 180 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 181 | let key_id = db |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 182 | .store_new_key( |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 183 | &key, |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 184 | KeyType::Client, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 185 | &key_parameters, |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 186 | &BlobInfo::new(&key_blob, &blob_metadata), |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 187 | &cert_info, |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 188 | &key_metadata, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 189 | &self.km_uuid, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 190 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 191 | .context(ks_err!())?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 192 | Ok(KeyDescriptor { |
| 193 | domain: Domain::KEY_ID, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 194 | nspace: key_id.id(), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 195 | ..Default::default() |
| 196 | }) |
| 197 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 198 | .context(ks_err!())?, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 201 | Ok(KeyMetadata { |
| 202 | key, |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 203 | keySecurityLevel: self.security_level, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 204 | certificate: cert_info.take_cert(), |
| 205 | certificateChain: cert_info.take_cert_chain(), |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 206 | authorizations: crate::utils::key_parameters_to_authorizations(key_parameters), |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 207 | modificationTimeMs: creation_date.to_millis_epoch(), |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 208 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 211 | fn create_operation( |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 212 | &self, |
| 213 | key: &KeyDescriptor, |
| 214 | operation_parameters: &[KeyParameter], |
| 215 | forced: bool, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 216 | ) -> Result<CreateOperationResponse> { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 217 | 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 Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 222 | let (km_blob, key_properties, key_id_guard, blob_metadata) = match key.domain { |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 223 | Domain::BLOB => { |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 224 | check_key_permission(KeyPerm::Use, key, &None) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 225 | .context(ks_err!("checking use permission for Domain::BLOB."))?; |
Janis Danisevskis | 186d9f4 | 2021-03-03 14:40:52 -0800 | [diff] [blame] | 226 | if forced { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 227 | check_key_permission(KeyPerm::ReqForcedOp, key, &None) |
| 228 | .context(ks_err!("checking forced permission for Domain::BLOB."))?; |
Janis Danisevskis | 186d9f4 | 2021-03-03 14:40:52 -0800 | [diff] [blame] | 229 | } |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 230 | ( |
| 231 | match &key.blob { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 232 | Some(blob) => blob, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 233 | None => { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 234 | return Err(Error::sys()).context(ks_err!( |
| 235 | "Key blob must be specified when \ |
| 236 | using Domain::BLOB." |
| 237 | )); |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 238 | } |
| 239 | }, |
| 240 | None, |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 241 | None, |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 242 | BlobMetaData::new(), |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 243 | ) |
| 244 | } |
| 245 | _ => { |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 246 | let super_key = SUPER_KEY |
| 247 | .read() |
| 248 | .unwrap() |
| 249 | .get_per_boot_key_by_user_id(uid_to_android_user(caller_uid)); |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 250 | let (key_id_guard, mut key_entry) = DB |
| 251 | .with::<_, Result<(KeyIdGuard, KeyEntry)>>(|db| { |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 252 | LEGACY_IMPORTER.with_try_import(key, caller_uid, super_key, || { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 253 | db.borrow_mut().load_key_entry( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 254 | key, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 255 | KeyType::Client, |
| 256 | KeyEntryLoadBits::KM, |
| 257 | caller_uid, |
Janis Danisevskis | 186d9f4 | 2021-03-03 14:40:52 -0800 | [diff] [blame] | 258 | |k, av| { |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 259 | check_key_permission(KeyPerm::Use, k, &av)?; |
Janis Danisevskis | 186d9f4 | 2021-03-03 14:40:52 -0800 | [diff] [blame] | 260 | if forced { |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 261 | check_key_permission(KeyPerm::ReqForcedOp, k, &av)?; |
Janis Danisevskis | 186d9f4 | 2021-03-03 14:40:52 -0800 | [diff] [blame] | 262 | } |
| 263 | Ok(()) |
| 264 | }, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 265 | ) |
| 266 | }) |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 267 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 268 | .context(ks_err!("Failed to load key blob."))?; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 269 | |
| 270 | let (blob, blob_metadata) = |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 271 | 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 Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 274 | ))?; |
| 275 | scoping_blob = blob; |
| 276 | |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame] | 277 | ( |
| 278 | &scoping_blob, |
| 279 | Some((key_id_guard.id(), key_entry.into_key_parameters())), |
| 280 | Some(key_id_guard), |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 281 | blob_metadata, |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame] | 282 | ) |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 283 | } |
| 284 | }; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 285 | |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 286 | let purpose = operation_parameters.iter().find(|p| p.tag == Tag::PURPOSE).map_or( |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 287 | Err(Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 288 | .context(ks_err!("No operation purpose specified.")), |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame] | 289 | |kp| match kp.value { |
| 290 | KeyParameterValue::KeyPurpose(p) => Ok(p), |
| 291 | _ => Err(Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 292 | .context(ks_err!("Malformed KeyParameter.")), |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame] | 293 | }, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 294 | )?; |
| 295 | |
Satya Tangirala | 2642ff9 | 2021-04-15 01:57:00 -0700 | [diff] [blame] | 296 | // 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 Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 302 | let (immediate_hat, mut auth_info) = ENFORCEMENTS |
| 303 | .authorize_create( |
| 304 | purpose, |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame] | 305 | key_properties.as_ref(), |
| 306 | operation_parameters.as_ref(), |
Janis Danisevskis | e3f7d20 | 2021-03-20 14:21:22 -0700 | [diff] [blame] | 307 | self.hw_info.timestampTokenRequired, |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 308 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 309 | .context(ks_err!())?; |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 310 | |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 311 | let km_blob = SUPER_KEY |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 312 | .read() |
| 313 | .unwrap() |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 314 | .unwrap_key_if_required(&blob_metadata, km_blob) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 315 | .context(ks_err!("Failed to handle super encryption."))?; |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 316 | |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 317 | let (begin_result, upgraded_blob) = self |
| 318 | .upgrade_keyblob_if_required_with( |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 319 | &*self.keymint, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 320 | key_id_guard, |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 321 | &km_blob, |
Max Bires | 55620ff | 2022-02-11 13:34:15 -0800 | [diff] [blame] | 322 | blob_metadata.km_uuid().copied(), |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 323 | operation_parameters, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 324 | |blob| loop { |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 325 | match map_km_error({ |
| 326 | let _wp = self.watch_millis( |
| 327 | "In KeystoreSecurityLevel::create_operation: calling begin", |
| 328 | 500, |
| 329 | ); |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 330 | self.keymint.begin( |
| 331 | purpose, |
| 332 | blob, |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 333 | operation_parameters, |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 334 | immediate_hat.as_ref(), |
| 335 | ) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 336 | }) { |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 337 | Err(Error::Km(ErrorCode::TOO_MANY_OPERATIONS)) => { |
Janis Danisevskis | 186d9f4 | 2021-03-03 14:40:52 -0800 | [diff] [blame] | 338 | self.operation_db.prune(caller_uid, forced)?; |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 339 | continue; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 340 | } |
Pavel Grafov | f45034a | 2021-05-12 22:35:45 +0100 | [diff] [blame] | 341 | 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 Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 353 | v => return v, |
| 354 | } |
| 355 | }, |
| 356 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 357 | .context(ks_err!("Failed to begin operation."))?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 358 | |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 359 | let operation_challenge = auth_info.finalize_create_authorization(begin_result.challenge); |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 360 | |
Hasini Gunasinghe | 0aba68a | 2021-03-19 00:43:52 +0000 | [diff] [blame] | 361 | let op_params: Vec<KeyParameter> = operation_parameters.to_vec(); |
| 362 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 363 | let operation = match begin_result.operation { |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 364 | 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 Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 372 | return Err(Error::sys()).context(ks_err!( |
| 373 | "Begin operation returned successfully, \ |
| 374 | but did not return a valid operation." |
| 375 | )); |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 376 | } |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 377 | }; |
| 378 | |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 379 | let op_binder: binder::Strong<dyn IKeystoreOperation> = |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 380 | KeystoreOperation::new_native_binder(operation) |
| 381 | .as_binder() |
| 382 | .into_interface() |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 383 | .context(ks_err!("Failed to create IKeystoreOperation."))?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 384 | |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 385 | Ok(CreateOperationResponse { |
| 386 | iOperation: Some(op_binder), |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 387 | operationChallenge: operation_challenge, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 388 | parameters: match begin_result.params.len() { |
| 389 | 0 => None, |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 390 | _ => Some(KeyParameters { keyParameter: begin_result.params }), |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 391 | }, |
Satya Tangirala | e2016a8 | 2021-03-05 09:28:30 -0800 | [diff] [blame] | 392 | // 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 Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 396 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 399 | fn add_required_parameters( |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 400 | &self, |
Janis Danisevskis | e766edc | 2021-02-06 12:16:26 -0800 | [diff] [blame] | 401 | uid: u32, |
| 402 | params: &[KeyParameter], |
| 403 | key: &KeyDescriptor, |
| 404 | ) -> Result<Vec<KeyParameter>> { |
Janis Danisevskis | 212c68b | 2021-01-14 22:29:28 -0800 | [diff] [blame] | 405 | let mut result = params.to_vec(); |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 406 | |
| 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 Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 410 | 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 Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Janis Danisevskis | 2b3c723 | 2021-12-20 13:16:23 -0800 | [diff] [blame] | 416 | // 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 Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 423 | .context(ks_err!( |
| 424 | "KeystoreSecurityLevel::add_required_parameters: \ |
| 425 | Failed to get epoch time." |
| 426 | ))? |
Janis Danisevskis | 2b3c723 | 2021-12-20 13:16:23 -0800 | [diff] [blame] | 427 | .as_millis() |
| 428 | .try_into() |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 429 | .context(ks_err!( |
| 430 | "KeystoreSecurityLevel::add_required_parameters: \ |
| 431 | Failed to convert epoch time." |
| 432 | ))?, |
Janis Danisevskis | 2b3c723 | 2021-12-20 13:16:23 -0800 | [diff] [blame] | 433 | ), |
| 434 | }); |
| 435 | } |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 436 | |
Janis Danisevskis | 2c08401 | 2021-01-31 22:23:17 -0800 | [diff] [blame] | 437 | // If there is an attestation challenge we need to get an application id. |
Janis Danisevskis | 212c68b | 2021-01-14 22:29:28 -0800 | [diff] [blame] | 438 | if params.iter().any(|kp| kp.tag == Tag::ATTESTATION_CHALLENGE) { |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 439 | let aaid = { |
| 440 | let _wp = self.watch_millis( |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 441 | "In KeystoreSecurityLevel::add_required_parameters calling: get_aaid", |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 442 | 500, |
| 443 | ); |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 444 | keystore2_aaid::get_aaid(uid) |
| 445 | .map_err(|e| anyhow!(ks_err!("get_aaid returned status {}.", e))) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 446 | }?; |
| 447 | |
Janis Danisevskis | 212c68b | 2021-01-14 22:29:28 -0800 | [diff] [blame] | 448 | result.push(KeyParameter { |
| 449 | tag: Tag::ATTESTATION_APPLICATION_ID, |
| 450 | value: KeyParameterValue::Blob(aaid), |
| 451 | }); |
| 452 | } |
Janis Danisevskis | 2c08401 | 2021-01-31 22:23:17 -0800 | [diff] [blame] | 453 | |
Janis Danisevskis | e766edc | 2021-02-06 12:16:26 -0800 | [diff] [blame] | 454 | if params.iter().any(|kp| kp.tag == Tag::INCLUDE_UNIQUE_ID) { |
Seth Moore | 66d9e90 | 2022-03-16 17:20:31 -0700 | [diff] [blame] | 455 | if check_key_permission(KeyPerm::GenUniqueId, key, &None).is_err() |
| 456 | && check_unique_id_attestation_permissions().is_err() |
| 457 | { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 458 | return Err(Error::perm()).context(ks_err!( |
| 459 | "Caller does not have the permission to generate a unique ID" |
| 460 | )); |
Seth Moore | 66d9e90 | 2022-03-16 17:20:31 -0700 | [diff] [blame] | 461 | } |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 462 | 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 Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 467 | result.push(KeyParameter { |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 468 | tag: Tag::RESET_SINCE_ID_ROTATION, |
| 469 | value: KeyParameterValue::BoolValue(true), |
| 470 | }) |
| 471 | } |
Janis Danisevskis | e766edc | 2021-02-06 12:16:26 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 474 | // 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 Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 477 | check_device_attestation_permissions().context(ks_err!( |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 478 | "Caller does not have the permission to attest device identifiers." |
| 479 | ))?; |
| 480 | } |
| 481 | |
Janis Danisevskis | 2c08401 | 2021-01-31 22:23:17 -0800 | [diff] [blame] | 482 | // 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 Danisevskis | 212c68b | 2021-01-14 22:29:28 -0800 | [diff] [blame] | 502 | Ok(result) |
| 503 | } |
| 504 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 505 | fn generate_key( |
| 506 | &self, |
| 507 | key: &KeyDescriptor, |
Shawn Willden | 8fde4c2 | 2021-02-14 13:58:22 -0700 | [diff] [blame] | 508 | attest_key_descriptor: Option<&KeyDescriptor>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 509 | params: &[KeyParameter], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 510 | flags: i32, |
Paul Crowley | d5653e5 | 2021-03-25 09:46:31 -0700 | [diff] [blame] | 511 | _entropy: &[u8], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 512 | ) -> Result<KeyMetadata> { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 513 | if key.domain != Domain::BLOB && key.alias.is_none() { |
| 514 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 515 | .context(ks_err!("Alias must be specified")); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 516 | } |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 517 | let caller_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 518 | |
| 519 | let key = match key.domain { |
| 520 | Domain::APP => KeyDescriptor { |
| 521 | domain: key.domain, |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 522 | nspace: caller_uid as i64, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 523 | alias: key.alias.clone(), |
| 524 | blob: None, |
| 525 | }, |
| 526 | _ => key.clone(), |
| 527 | }; |
| 528 | |
| 529 | // generate_key requires the rebind permission. |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 530 | // Must return on error for security reasons. |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 531 | check_key_permission(KeyPerm::Rebind, &key, &None).context(ks_err!())?; |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 532 | |
| 533 | let attestation_key_info = match (key.domain, attest_key_descriptor) { |
| 534 | (Domain::BLOB, _) => None, |
Satya Tangirala | fdb9c76 | 2021-03-09 22:34:22 -0800 | [diff] [blame] | 535 | _ => DB |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 536 | .with(|db| { |
| 537 | get_attest_key_info( |
Satya Tangirala | fdb9c76 | 2021-03-09 22:34:22 -0800 | [diff] [blame] | 538 | &key, |
| 539 | caller_uid, |
| 540 | attest_key_descriptor, |
| 541 | params, |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 542 | &self.rem_prov_state, |
Satya Tangirala | fdb9c76 | 2021-03-09 22:34:22 -0800 | [diff] [blame] | 543 | &mut db.borrow_mut(), |
| 544 | ) |
| 545 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 546 | .context(ks_err!("Trying to get an attestation key"))?, |
Satya Tangirala | fdb9c76 | 2021-03-09 22:34:22 -0800 | [diff] [blame] | 547 | }; |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 548 | let params = self |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 549 | .add_required_parameters(caller_uid, params, &key) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 550 | .context(ks_err!("Trying to get aaid."))?; |
Janis Danisevskis | 212c68b | 2021-01-14 22:29:28 -0800 | [diff] [blame] | 551 | |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 552 | 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 Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 560 | &*self.keymint, |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 561 | Some(key_id_guard), |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 562 | &KeyBlob::Ref(&blob), |
Max Bires | 55620ff | 2022-02-11 13:34:15 -0800 | [diff] [blame] | 563 | blob_metadata.km_uuid().copied(), |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 564 | ¶ms, |
| 565 | |blob| { |
| 566 | let attest_key = Some(AttestationKey { |
| 567 | keyBlob: blob.to_vec(), |
| 568 | attestKeyParams: vec![], |
| 569 | issuerSubjectName: issuer_subject.clone(), |
| 570 | }); |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 571 | 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 Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 579 | self.keymint.generateKey(¶ms, attest_key.as_ref()) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 580 | }) |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 581 | }, |
| 582 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 583 | .context(ks_err!("Using user generated attestation key.")) |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 584 | .map(|(result, _)| result), |
Max Bires | 55620ff | 2022-02-11 13:34:15 -0800 | [diff] [blame] | 585 | 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(¶ms, dynamic_attest_key.as_ref()) |
| 611 | }) |
| 612 | }, |
| 613 | ) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 614 | .context("While generating Key with remote provisioned attestation key.") |
Max Bires | 55620ff | 2022-02-11 13:34:15 -0800 | [diff] [blame] | 615 | .map(|(mut result, _)| { |
| 616 | result.certificateChain.push(attestation_certs); |
| 617 | result |
| 618 | }), |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 619 | 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 Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 627 | self.keymint.generateKey(¶ms, None) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 628 | }) |
| 629 | .context("While generating Key without explicit attestation key."), |
Max Bires | 97f9681 | 2021-02-23 23:44:57 -0800 | [diff] [blame] | 630 | } |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 631 | .context(ks_err!())?; |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 632 | |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 633 | let user_id = uid_to_android_user(caller_uid); |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 634 | self.store_new_key(key, creation_result, user_id, Some(flags)).context(ks_err!()) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | fn import_key( |
| 638 | &self, |
| 639 | key: &KeyDescriptor, |
Paul Crowley | d5653e5 | 2021-03-25 09:46:31 -0700 | [diff] [blame] | 640 | _attestation_key: Option<&KeyDescriptor>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 641 | params: &[KeyParameter], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 642 | flags: i32, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 643 | key_data: &[u8], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 644 | ) -> Result<KeyMetadata> { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 645 | if key.domain != Domain::BLOB && key.alias.is_none() { |
| 646 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 647 | .context(ks_err!("Alias must be specified")); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 648 | } |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 649 | let caller_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 650 | |
| 651 | let key = match key.domain { |
| 652 | Domain::APP => KeyDescriptor { |
| 653 | domain: key.domain, |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 654 | nspace: caller_uid as i64, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 655 | alias: key.alias.clone(), |
| 656 | blob: None, |
| 657 | }, |
| 658 | _ => key.clone(), |
| 659 | }; |
| 660 | |
| 661 | // import_key requires the rebind permission. |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 662 | check_key_permission(KeyPerm::Rebind, &key, &None).context("In import_key.")?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 663 | |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 664 | let params = self |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 665 | .add_required_parameters(caller_uid, params, &key) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 666 | .context(ks_err!("Trying to get aaid."))?; |
Janis Danisevskis | 212c68b | 2021-01-14 22:29:28 -0800 | [diff] [blame] | 667 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 668 | let format = params |
| 669 | .iter() |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 670 | .find(|p| p.tag == Tag::ALGORITHM) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 671 | .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
| 672 | .context("No KeyParameter 'Algorithm'.") |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame] | 673 | .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 Johnson | aec2eca | 2022-11-30 17:08:05 +0000 | [diff] [blame^] | 680 | .context(ks_err!("Unknown Algorithm {:?}.", v)), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 681 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 682 | .context(ks_err!())?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 683 | |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 684 | let km_dev = &self.keymint; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 685 | let creation_result = map_km_error({ |
| 686 | let _wp = |
| 687 | self.watch_millis("In KeystoreSecurityLevel::import_key: calling importKey.", 500); |
| 688 | km_dev.importKey(¶ms, format, key_data, None /* attestKey */) |
| 689 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 690 | .context(ks_err!("Trying to call importKey"))?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 691 | |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 692 | let user_id = uid_to_android_user(caller_uid); |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 693 | self.store_new_key(key, creation_result, user_id, Some(flags)).context(ks_err!()) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 694 | } |
| 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 Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 703 | ) -> Result<KeyMetadata> { |
Janis Danisevskis | 32adc7d | 2021-02-07 14:04:01 -0800 | [diff] [blame] | 704 | 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 Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 710 | 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 Danisevskis | 32adc7d | 2021-02-07 14:04:01 -0800 | [diff] [blame] | 712 | key |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 713 | )); |
Janis Danisevskis | 32adc7d | 2021-02-07 14:04:01 -0800 | [diff] [blame] | 714 | } |
| 715 | }; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 716 | |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 717 | if wrapping_key.domain == Domain::BLOB { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 718 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
| 719 | .context(ks_err!("Import wrapped key not supported for self managed blobs.")); |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 722 | let caller_uid = ThreadState::get_calling_uid(); |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 723 | let user_id = uid_to_android_user(caller_uid); |
| 724 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 725 | let key = match key.domain { |
| 726 | Domain::APP => KeyDescriptor { |
| 727 | domain: key.domain, |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 728 | nspace: caller_uid as i64, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 729 | alias: key.alias.clone(), |
| 730 | blob: None, |
| 731 | }, |
Janis Danisevskis | 32adc7d | 2021-02-07 14:04:01 -0800 | [diff] [blame] | 732 | Domain::SELINUX => KeyDescriptor { |
| 733 | domain: Domain::SELINUX, |
| 734 | nspace: key.nspace, |
| 735 | alias: key.alias.clone(), |
| 736 | blob: None, |
| 737 | }, |
| 738 | _ => panic!("Unreachable."), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 739 | }; |
| 740 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 741 | // Import_wrapped_key requires the rebind permission for the new key. |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 742 | check_key_permission(KeyPerm::Rebind, &key, &None).context(ks_err!())?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 743 | |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 744 | let super_key = SUPER_KEY.read().unwrap().get_per_boot_key_by_user_id(user_id); |
| 745 | |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 746 | let (wrapping_key_id_guard, mut wrapping_key_entry) = DB |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 747 | .with(|db| { |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 748 | LEGACY_IMPORTER.with_try_import(&key, caller_uid, super_key, || { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 749 | db.borrow_mut().load_key_entry( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 750 | wrapping_key, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 751 | KeyType::Client, |
| 752 | KeyEntryLoadBits::KM, |
| 753 | caller_uid, |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 754 | |k, av| check_key_permission(KeyPerm::Use, k, &av), |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 755 | ) |
| 756 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 757 | }) |
| 758 | .context("Failed to load wrapping key.")?; |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 759 | |
Shaquille Johnson | aec2eca | 2022-11-30 17:08:05 +0000 | [diff] [blame^] | 760 | 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 Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 764 | |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 765 | let wrapping_key_blob = SUPER_KEY |
| 766 | .read() |
| 767 | .unwrap() |
| 768 | .unwrap_key_if_required(&wrapping_blob_metadata, &wrapping_key_blob) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 769 | .context(ks_err!("Failed to handle super encryption for wrapping key."))?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 770 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 771 | // 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 Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 778 | HardwareAuthenticatorType::PASSWORD => Some(a.authenticatorId), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 779 | _ => None, |
| 780 | }) |
Janis Danisevskis | 32adc7d | 2021-02-07 14:04:01 -0800 | [diff] [blame] | 781 | .unwrap_or(-1); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 782 | |
| 783 | let fp_sid = authenticators |
| 784 | .iter() |
| 785 | .find_map(|a| match a.authenticatorType { |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 786 | HardwareAuthenticatorType::FINGERPRINT => Some(a.authenticatorId), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 787 | _ => None, |
| 788 | }) |
Janis Danisevskis | 32adc7d | 2021-02-07 14:04:01 -0800 | [diff] [blame] | 789 | .unwrap_or(-1); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 790 | |
| 791 | let masking_key = masking_key.unwrap_or(ZERO_BLOB_32); |
| 792 | |
Janis Danisevskis | 104d8e4 | 2021-01-14 22:49:27 -0800 | [diff] [blame] | 793 | let (creation_result, _) = self |
| 794 | .upgrade_keyblob_if_required_with( |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 795 | &*self.keymint, |
Janis Danisevskis | 104d8e4 | 2021-01-14 22:49:27 -0800 | [diff] [blame] | 796 | Some(wrapping_key_id_guard), |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 797 | &wrapping_key_blob, |
Max Bires | 55620ff | 2022-02-11 13:34:15 -0800 | [diff] [blame] | 798 | wrapping_blob_metadata.km_uuid().copied(), |
Janis Danisevskis | 104d8e4 | 2021-01-14 22:49:27 -0800 | [diff] [blame] | 799 | &[], |
| 800 | |wrapping_blob| { |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 801 | let _wp = self.watch_millis( |
| 802 | "In KeystoreSecurityLevel::import_wrapped_key: calling importWrappedKey.", |
| 803 | 500, |
| 804 | ); |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 805 | let creation_result = map_km_error(self.keymint.importWrappedKey( |
Janis Danisevskis | 104d8e4 | 2021-01-14 22:49:27 -0800 | [diff] [blame] | 806 | wrapped_data, |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 807 | wrapping_blob, |
Janis Danisevskis | 104d8e4 | 2021-01-14 22:49:27 -0800 | [diff] [blame] | 808 | masking_key, |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 809 | params, |
Janis Danisevskis | 104d8e4 | 2021-01-14 22:49:27 -0800 | [diff] [blame] | 810 | pw_sid, |
| 811 | fp_sid, |
| 812 | ))?; |
| 813 | Ok(creation_result) |
| 814 | }, |
| 815 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 816 | .context(ks_err!())?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 817 | |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 818 | self.store_new_key(key, creation_result, user_id, None) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 819 | .context(ks_err!("Trying to store the new key.")) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 820 | } |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 821 | |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 822 | fn store_upgraded_keyblob( |
| 823 | key_id_guard: KeyIdGuard, |
Max Bires | 55620ff | 2022-02-11 13:34:15 -0800 | [diff] [blame] | 824 | km_uuid: Option<Uuid>, |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 825 | key_blob: &KeyBlob, |
| 826 | upgraded_blob: &[u8], |
| 827 | ) -> Result<()> { |
| 828 | let (upgraded_blob_to_be_stored, new_blob_metadata) = |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 829 | SuperKeyManager::reencrypt_if_required(key_blob, upgraded_blob) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 830 | .context(ks_err!("Failed to handle super encryption."))?; |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 831 | |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 832 | let mut new_blob_metadata = new_blob_metadata.unwrap_or_default(); |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 833 | if let Some(uuid) = km_uuid { |
Max Bires | 55620ff | 2022-02-11 13:34:15 -0800 | [diff] [blame] | 834 | new_blob_metadata.add(BlobMetaEntry::KmUuid(uuid)); |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 835 | } |
| 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 Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 846 | .context(ks_err!("Failed to insert upgraded blob into the database.")) |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 849 | fn upgrade_keyblob_if_required_with<T, F>( |
| 850 | &self, |
| 851 | km_dev: &dyn IKeyMintDevice, |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 852 | mut key_id_guard: Option<KeyIdGuard>, |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 853 | key_blob: &KeyBlob, |
Max Bires | 55620ff | 2022-02-11 13:34:15 -0800 | [diff] [blame] | 854 | km_uuid: Option<Uuid>, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 855 | params: &[KeyParameter], |
| 856 | f: F, |
| 857 | ) -> Result<(T, Option<Vec<u8>>)> |
| 858 | where |
| 859 | F: Fn(&[u8]) -> Result<T, Error>, |
| 860 | { |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 861 | 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 Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 870 | Self::store_upgraded_keyblob(kid, km_uuid, key_blob, upgraded_blob) |
| 871 | .context(ks_err!("store_upgraded_keyblob failed")) |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 872 | } else { |
| 873 | Ok(()) |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 874 | } |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 875 | }, |
| 876 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 877 | .context(ks_err!())?; |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 878 | |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 879 | // 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 Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 884 | Self::store_upgraded_keyblob(kid, km_uuid, key_blob, key_blob) |
| 885 | .context(ks_err!("store_upgraded_keyblob failed in forced reencrypt"))?; |
Paul Crowley | 8d5b253 | 2021-03-19 10:53:07 -0700 | [diff] [blame] | 886 | } |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 887 | } |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 888 | Ok((v, upgraded_blob)) |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 889 | } |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 890 | |
Janis Danisevskis | b2434d0 | 2021-04-20 12:49:27 -0700 | [diff] [blame] | 891 | fn convert_storage_key_to_ephemeral( |
| 892 | &self, |
| 893 | storage_key: &KeyDescriptor, |
| 894 | ) -> Result<EphemeralStorageKeyResponse> { |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 895 | if storage_key.domain != Domain::BLOB { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 896 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
| 897 | .context(ks_err!("Key must be of Domain::BLOB")); |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 898 | } |
| 899 | let key_blob = storage_key |
| 900 | .blob |
| 901 | .as_ref() |
| 902 | .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 903 | .context(ks_err!("No key blob specified"))?; |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 904 | |
| 905 | // convert_storage_key_to_ephemeral requires the associated permission |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 906 | check_key_permission(KeyPerm::ConvertStorageKeyToEphemeral, storage_key, &None) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 907 | .context(ks_err!("Check permission"))?; |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 908 | |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 909 | let km_dev = &self.keymint; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 910 | 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 Danisevskis | b2434d0 | 2021-04-20 12:49:27 -0700 | [diff] [blame] | 920 | Ok(result) => { |
| 921 | Ok(EphemeralStorageKeyResponse { ephemeralKey: result, upgradedBlob: None }) |
| 922 | } |
| 923 | Err(error::Error::Km(ErrorCode::KEY_REQUIRES_UPGRADE)) => { |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 924 | 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 Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 931 | .context(ks_err!("Failed to upgrade key blob."))?; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 932 | let ephemeral_key = { |
| 933 | let _wp = self.watch_millis( |
| 934 | "In convert_storage_key_to_ephemeral: calling convertStorageKeyToEphemeral (2)", |
| 935 | 500, |
| 936 | ); |
Janis Danisevskis | 84af4d1 | 2021-07-22 17:39:15 -0700 | [diff] [blame] | 937 | map_km_error(km_dev.convertStorageKeyToEphemeral(&upgraded_blob)) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 938 | } |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 939 | .context(ks_err!( |
Janis Danisevskis | b2434d0 | 2021-04-20 12:49:27 -0700 | [diff] [blame] | 940 | "Failed to retrieve ephemeral key (after upgrade)." |
| 941 | ))?; |
| 942 | Ok(EphemeralStorageKeyResponse { |
| 943 | ephemeralKey: ephemeral_key, |
| 944 | upgradedBlob: Some(upgraded_blob), |
| 945 | }) |
| 946 | } |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 947 | Err(e) => Err(e).context(ks_err!("Failed to retrieve ephemeral key.")), |
Janis Danisevskis | b2434d0 | 2021-04-20 12:49:27 -0700 | [diff] [blame] | 948 | } |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 949 | } |
Satya Tangirala | 04bca0d | 2021-03-08 22:27:54 -0800 | [diff] [blame] | 950 | |
| 951 | fn delete_key(&self, key: &KeyDescriptor) -> Result<()> { |
| 952 | if key.domain != Domain::BLOB { |
| 953 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 954 | .context(ks_err!("delete_key: Key must be of Domain::BLOB")); |
Satya Tangirala | 04bca0d | 2021-03-08 22:27:54 -0800 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | let key_blob = key |
| 958 | .blob |
| 959 | .as_ref() |
| 960 | .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 961 | .context(ks_err!("delete_key: No key blob specified"))?; |
Satya Tangirala | 04bca0d | 2021-03-08 22:27:54 -0800 | [diff] [blame] | 962 | |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 963 | check_key_permission(KeyPerm::Delete, key, &None) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 964 | .context(ks_err!("delete_key: Checking delete permissions"))?; |
Satya Tangirala | 04bca0d | 2021-03-08 22:27:54 -0800 | [diff] [blame] | 965 | |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 966 | let km_dev = &self.keymint; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 967 | { |
| 968 | let _wp = |
| 969 | self.watch_millis("In KeystoreSecuritylevel::delete_key: calling deleteKey", 500); |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 970 | map_km_error(km_dev.deleteKey(key_blob)).context(ks_err!("keymint device deleteKey")) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 971 | } |
Satya Tangirala | 04bca0d | 2021-03-08 22:27:54 -0800 | [diff] [blame] | 972 | } |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | impl binder::Interface for KeystoreSecurityLevel {} |
| 976 | |
| 977 | impl IKeystoreSecurityLevel for KeystoreSecurityLevel { |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 978 | fn createOperation( |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 979 | &self, |
| 980 | key: &KeyDescriptor, |
| 981 | operation_parameters: &[KeyParameter], |
| 982 | forced: bool, |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 983 | ) -> binder::Result<CreateOperationResponse> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 984 | let _wp = self.watch_millis("IKeystoreSecurityLevel::createOperation", 500); |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 985 | map_or_log_err(self.create_operation(key, operation_parameters, forced), Ok) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 986 | } |
| 987 | fn generateKey( |
| 988 | &self, |
| 989 | key: &KeyDescriptor, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 990 | attestation_key: Option<&KeyDescriptor>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 991 | params: &[KeyParameter], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 992 | flags: i32, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 993 | entropy: &[u8], |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 994 | ) -> binder::Result<KeyMetadata> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 995 | // 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 Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 998 | let result = self.generate_key(key, attestation_key, params, flags, entropy); |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 999 | log_key_creation_event_stats(self.security_level, params, &result); |
Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 1000 | log_key_generated(key, ThreadState::get_calling_uid(), result.is_ok()); |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 1001 | map_or_log_err(result, Ok) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1002 | } |
| 1003 | fn importKey( |
| 1004 | &self, |
| 1005 | key: &KeyDescriptor, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 1006 | attestation_key: Option<&KeyDescriptor>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1007 | params: &[KeyParameter], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 1008 | flags: i32, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1009 | key_data: &[u8], |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1010 | ) -> binder::Result<KeyMetadata> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 1011 | let _wp = self.watch_millis("IKeystoreSecurityLevel::importKey", 500); |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 1012 | let result = self.import_key(key, attestation_key, params, flags, key_data); |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 1013 | log_key_creation_event_stats(self.security_level, params, &result); |
Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 1014 | log_key_imported(key, ThreadState::get_calling_uid(), result.is_ok()); |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 1015 | map_or_log_err(result, Ok) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1016 | } |
| 1017 | fn importWrappedKey( |
| 1018 | &self, |
| 1019 | key: &KeyDescriptor, |
| 1020 | wrapping_key: &KeyDescriptor, |
| 1021 | masking_key: Option<&[u8]>, |
| 1022 | params: &[KeyParameter], |
| 1023 | authenticators: &[AuthenticatorSpec], |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1024 | ) -> binder::Result<KeyMetadata> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 1025 | let _wp = self.watch_millis("IKeystoreSecurityLevel::importWrappedKey", 500); |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 1026 | let result = |
| 1027 | self.import_wrapped_key(key, wrapping_key, masking_key, params, authenticators); |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 1028 | log_key_creation_event_stats(self.security_level, params, &result); |
Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 1029 | log_key_imported(key, ThreadState::get_calling_uid(), result.is_ok()); |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 1030 | map_or_log_err(result, Ok) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1031 | } |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 1032 | fn convertStorageKeyToEphemeral( |
| 1033 | &self, |
| 1034 | storage_key: &KeyDescriptor, |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1035 | ) -> binder::Result<EphemeralStorageKeyResponse> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 1036 | let _wp = self.watch_millis("IKeystoreSecurityLevel::convertStorageKeyToEphemeral", 500); |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 1037 | map_or_log_err(self.convert_storage_key_to_ephemeral(storage_key), Ok) |
| 1038 | } |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1039 | fn deleteKey(&self, key: &KeyDescriptor) -> binder::Result<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 1040 | let _wp = self.watch_millis("IKeystoreSecurityLevel::deleteKey", 500); |
Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 1041 | 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 Tangirala | 04bca0d | 2021-03-08 22:27:54 -0800 | [diff] [blame] | 1044 | } |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1045 | } |