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}; |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 22 | use crate::error::{ |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 23 | self, into_logged_binder, map_km_error, wrapped_rkpd_error_to_ks_error, Error, ErrorCode, |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 24 | }; |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 25 | use crate::globals::{ |
| 26 | get_remotely_provisioned_component_name, DB, ENFORCEMENTS, LEGACY_IMPORTER, SUPER_KEY, |
| 27 | }; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 28 | use crate::key_parameter::KeyParameter as KsKeyParam; |
| 29 | use crate::key_parameter::KeyParameterValue as KsKeyParamValue; |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 30 | use crate::ks_err; |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 31 | use crate::metrics_store::log_key_creation_event_stats; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 32 | use crate::remote_provisioning::RemProvState; |
| 33 | use crate::super_key::{KeyBlob, SuperKeyManager}; |
| 34 | use crate::utils::{ |
Seth Moore | 66d9e90 | 2022-03-16 17:20:31 -0700 | [diff] [blame] | 35 | check_device_attestation_permissions, check_key_permission, |
| 36 | check_unique_id_attestation_permissions, is_device_id_attestation_tag, |
Shaquille Johnson | 9a587a2 | 2024-02-28 19:54:24 +0000 | [diff] [blame] | 37 | key_characteristics_to_internal, log_security_safe_params, uid_to_android_user, watchdog as wd, |
| 38 | UNDEFINED_NOT_AFTER, |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 39 | }; |
| 40 | use crate::{ |
| 41 | database::{ |
| 42 | BlobMetaData, BlobMetaEntry, DateTime, KeyEntry, KeyEntryLoadBits, KeyMetaData, |
| 43 | KeyMetaEntry, KeyType, SubComponentType, Uuid, |
| 44 | }, |
| 45 | operation::KeystoreOperation, |
| 46 | operation::LoggingInfo, |
| 47 | operation::OperationDb, |
| 48 | permission::KeyPerm, |
| 49 | }; |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 50 | use crate::{globals::get_keymint_device, id_rotation::IdRotationState}; |
Shawn Willden | 708744a | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 51 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 52 | Algorithm::Algorithm, AttestationKey::AttestationKey, |
Shawn Willden | 8fde4c2 | 2021-02-14 13:58:22 -0700 | [diff] [blame] | 53 | HardwareAuthenticatorType::HardwareAuthenticatorType, IKeyMintDevice::IKeyMintDevice, |
| 54 | KeyCreationResult::KeyCreationResult, KeyFormat::KeyFormat, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 55 | KeyMintHardwareInfo::KeyMintHardwareInfo, KeyParameter::KeyParameter, |
| 56 | KeyParameterValue::KeyParameterValue, SecurityLevel::SecurityLevel, Tag::Tag, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 57 | }; |
Andrew Walbran | de45c8b | 2021-04-13 14:42:38 +0000 | [diff] [blame] | 58 | use android_hardware_security_keymint::binder::{BinderFeatures, Strong, ThreadState}; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 59 | use android_system_keystore2::aidl::android::system::keystore2::{ |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 60 | AuthenticatorSpec::AuthenticatorSpec, CreateOperationResponse::CreateOperationResponse, |
Janis Danisevskis | b2434d0 | 2021-04-20 12:49:27 -0700 | [diff] [blame] | 61 | Domain::Domain, EphemeralStorageKeyResponse::EphemeralStorageKeyResponse, |
| 62 | IKeystoreOperation::IKeystoreOperation, IKeystoreSecurityLevel::BnKeystoreSecurityLevel, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 63 | IKeystoreSecurityLevel::IKeystoreSecurityLevel, KeyDescriptor::KeyDescriptor, |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 64 | KeyMetadata::KeyMetadata, KeyParameters::KeyParameters, ResponseCode::ResponseCode, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 65 | }; |
Janis Danisevskis | 212c68b | 2021-01-14 22:29:28 -0800 | [diff] [blame] | 66 | use anyhow::{anyhow, Context, Result}; |
Alice Wang | 01c16b6 | 2023-11-07 14:27:49 +0000 | [diff] [blame] | 67 | use rkpd_client::store_rkpd_attestation_key; |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 68 | use std::convert::TryInto; |
| 69 | use std::time::SystemTime; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 70 | |
| 71 | /// Implementation of the IKeystoreSecurityLevel Interface. |
| 72 | pub struct KeystoreSecurityLevel { |
| 73 | security_level: SecurityLevel, |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 74 | keymint: Strong<dyn IKeyMintDevice>, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 75 | hw_info: KeyMintHardwareInfo, |
| 76 | km_uuid: Uuid, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 77 | operation_db: OperationDb, |
Max Bires | 97f9681 | 2021-02-23 23:44:57 -0800 | [diff] [blame] | 78 | rem_prov_state: RemProvState, |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 79 | id_rotation_state: IdRotationState, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 82 | // Blob of 32 zeroes used as empty masking key. |
| 83 | static ZERO_BLOB_32: &[u8] = &[0; 32]; |
| 84 | |
| 85 | impl KeystoreSecurityLevel { |
| 86 | /// Creates a new security level instance wrapped in a |
Andrew Walbran | de45c8b | 2021-04-13 14:42:38 +0000 | [diff] [blame] | 87 | /// BnKeystoreSecurityLevel proxy object. It also enables |
| 88 | /// `BinderFeatures::set_requesting_sid` on the new interface, because |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 89 | /// we need it for checking keystore permissions. |
| 90 | pub fn new_native_binder( |
| 91 | security_level: SecurityLevel, |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 92 | id_rotation_state: IdRotationState, |
Stephen Crane | 221bbb5 | 2020-12-16 15:52:10 -0800 | [diff] [blame] | 93 | ) -> Result<(Strong<dyn IKeystoreSecurityLevel>, Uuid)> { |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 94 | let (dev, hw_info, km_uuid) = get_keymint_device(&security_level) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 95 | .context(ks_err!("KeystoreSecurityLevel::new_native_binder."))?; |
Andrew Walbran | de45c8b | 2021-04-13 14:42:38 +0000 | [diff] [blame] | 96 | let result = BnKeystoreSecurityLevel::new_binder( |
| 97 | Self { |
| 98 | security_level, |
| 99 | keymint: dev, |
| 100 | hw_info, |
| 101 | km_uuid, |
| 102 | operation_db: OperationDb::new(), |
David Drysdale | 8c4c4f3 | 2023-10-31 12:14:11 +0000 | [diff] [blame] | 103 | rem_prov_state: RemProvState::new(security_level), |
Andrew Walbran | de45c8b | 2021-04-13 14:42:38 +0000 | [diff] [blame] | 104 | id_rotation_state, |
| 105 | }, |
| 106 | BinderFeatures { set_requesting_sid: true, ..BinderFeatures::default() }, |
| 107 | ); |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 108 | Ok((result, km_uuid)) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 111 | fn watch_millis(&self, id: &'static str, millis: u64) -> Option<wd::WatchPoint> { |
| 112 | let sec_level = self.security_level; |
David Drysdale | 387c85b | 2024-06-10 14:40:45 +0100 | [diff] [blame] | 113 | wd::watch_millis_with(id, millis, sec_level) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 114 | } |
| 115 | |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 116 | fn watch(&self, id: &'static str) -> Option<wd::WatchPoint> { |
| 117 | let sec_level = self.security_level; |
David Drysdale | 387c85b | 2024-06-10 14:40:45 +0100 | [diff] [blame] | 118 | wd::watch_millis_with(id, wd::DEFAULT_TIMEOUT_MS, sec_level) |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 121 | fn store_new_key( |
| 122 | &self, |
| 123 | key: KeyDescriptor, |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 124 | creation_result: KeyCreationResult, |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 125 | user_id: u32, |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 126 | flags: Option<i32>, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 127 | ) -> Result<KeyMetadata> { |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 128 | let KeyCreationResult { |
| 129 | keyBlob: key_blob, |
| 130 | keyCharacteristics: key_characteristics, |
| 131 | certificateChain: mut certificate_chain, |
| 132 | } = creation_result; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 133 | |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 134 | let mut cert_info: CertificateInfo = CertificateInfo::new( |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 135 | match certificate_chain.len() { |
| 136 | 0 => None, |
| 137 | _ => Some(certificate_chain.remove(0).encodedCertificate), |
| 138 | }, |
| 139 | match certificate_chain.len() { |
| 140 | 0 => None, |
| 141 | _ => Some( |
| 142 | certificate_chain |
| 143 | .iter() |
Charisee | a1e1c48 | 2022-02-26 01:26:35 +0000 | [diff] [blame] | 144 | .flat_map(|c| c.encodedCertificate.iter()) |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 145 | .copied() |
| 146 | .collect(), |
| 147 | ), |
| 148 | }, |
| 149 | ); |
| 150 | |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 151 | let mut key_parameters = key_characteristics_to_internal(key_characteristics); |
| 152 | |
| 153 | key_parameters.push(KsKeyParam::new( |
| 154 | KsKeyParamValue::UserID(user_id as i32), |
| 155 | SecurityLevel::SOFTWARE, |
| 156 | )); |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 157 | |
Shaquille Johnson | aec2eca | 2022-11-30 17:08:05 +0000 | [diff] [blame] | 158 | 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] | 159 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 160 | let key = match key.domain { |
Satya Tangirala | 60671e3 | 2021-03-04 16:12:19 -0800 | [diff] [blame] | 161 | Domain::BLOB => KeyDescriptor { |
| 162 | domain: Domain::BLOB, |
| 163 | blob: Some(key_blob.to_vec()), |
| 164 | ..Default::default() |
| 165 | }, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 166 | _ => DB |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 167 | .with::<_, Result<KeyDescriptor>>(|db| { |
Satya Tangirala | 60671e3 | 2021-03-04 16:12:19 -0800 | [diff] [blame] | 168 | let mut db = db.borrow_mut(); |
| 169 | |
| 170 | let (key_blob, mut blob_metadata) = SUPER_KEY |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 171 | .read() |
| 172 | .unwrap() |
Satya Tangirala | 60671e3 | 2021-03-04 16:12:19 -0800 | [diff] [blame] | 173 | .handle_super_encryption_on_key_init( |
| 174 | &mut db, |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 175 | &LEGACY_IMPORTER, |
Satya Tangirala | 60671e3 | 2021-03-04 16:12:19 -0800 | [diff] [blame] | 176 | &(key.domain), |
| 177 | &key_parameters, |
| 178 | flags, |
| 179 | user_id, |
| 180 | &key_blob, |
| 181 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 182 | .context(ks_err!("Failed to handle super encryption."))?; |
Satya Tangirala | 60671e3 | 2021-03-04 16:12:19 -0800 | [diff] [blame] | 183 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 184 | let mut key_metadata = KeyMetaData::new(); |
| 185 | key_metadata.add(KeyMetaEntry::CreationDate(creation_date)); |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 186 | blob_metadata.add(BlobMetaEntry::KmUuid(self.km_uuid)); |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 187 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 188 | let key_id = db |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 189 | .store_new_key( |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 190 | &key, |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 191 | KeyType::Client, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 192 | &key_parameters, |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 193 | &BlobInfo::new(&key_blob, &blob_metadata), |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 194 | &cert_info, |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 195 | &key_metadata, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 196 | &self.km_uuid, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 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 | Ok(KeyDescriptor { |
| 200 | domain: Domain::KEY_ID, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 201 | nspace: key_id.id(), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 202 | ..Default::default() |
| 203 | }) |
| 204 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 205 | .context(ks_err!())?, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 206 | }; |
| 207 | |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 208 | Ok(KeyMetadata { |
| 209 | key, |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 210 | keySecurityLevel: self.security_level, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 211 | certificate: cert_info.take_cert(), |
| 212 | certificateChain: cert_info.take_cert_chain(), |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 213 | authorizations: crate::utils::key_parameters_to_authorizations(key_parameters), |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 214 | modificationTimeMs: creation_date.to_millis_epoch(), |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 215 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 218 | fn create_operation( |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 219 | &self, |
| 220 | key: &KeyDescriptor, |
| 221 | operation_parameters: &[KeyParameter], |
| 222 | forced: bool, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 223 | ) -> Result<CreateOperationResponse> { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 224 | let caller_uid = ThreadState::get_calling_uid(); |
| 225 | // We use `scoping_blob` to extend the life cycle of the blob loaded from the database, |
| 226 | // so that we can use it by reference like the blob provided by the key descriptor. |
| 227 | // Otherwise, we would have to clone the blob from the key descriptor. |
| 228 | let scoping_blob: Vec<u8>; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 229 | 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] | 230 | Domain::BLOB => { |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 231 | check_key_permission(KeyPerm::Use, key, &None) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 232 | .context(ks_err!("checking use permission for Domain::BLOB."))?; |
Janis Danisevskis | 186d9f4 | 2021-03-03 14:40:52 -0800 | [diff] [blame] | 233 | if forced { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 234 | check_key_permission(KeyPerm::ReqForcedOp, key, &None) |
| 235 | .context(ks_err!("checking forced permission for Domain::BLOB."))?; |
Janis Danisevskis | 186d9f4 | 2021-03-03 14:40:52 -0800 | [diff] [blame] | 236 | } |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 237 | ( |
| 238 | match &key.blob { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 239 | Some(blob) => blob, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 240 | None => { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 241 | return Err(Error::sys()).context(ks_err!( |
| 242 | "Key blob must be specified when \ |
| 243 | using Domain::BLOB." |
| 244 | )); |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 245 | } |
| 246 | }, |
| 247 | None, |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 248 | None, |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 249 | BlobMetaData::new(), |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 250 | ) |
| 251 | } |
| 252 | _ => { |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 253 | let super_key = SUPER_KEY |
| 254 | .read() |
| 255 | .unwrap() |
Eric Biggers | 673d34a | 2023-10-18 01:54:18 +0000 | [diff] [blame] | 256 | .get_after_first_unlock_key_by_user_id(uid_to_android_user(caller_uid)); |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 257 | let (key_id_guard, mut key_entry) = DB |
| 258 | .with::<_, Result<(KeyIdGuard, KeyEntry)>>(|db| { |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 259 | LEGACY_IMPORTER.with_try_import(key, caller_uid, super_key, || { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 260 | db.borrow_mut().load_key_entry( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 261 | key, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 262 | KeyType::Client, |
| 263 | KeyEntryLoadBits::KM, |
| 264 | caller_uid, |
Janis Danisevskis | 186d9f4 | 2021-03-03 14:40:52 -0800 | [diff] [blame] | 265 | |k, av| { |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 266 | check_key_permission(KeyPerm::Use, k, &av)?; |
Janis Danisevskis | 186d9f4 | 2021-03-03 14:40:52 -0800 | [diff] [blame] | 267 | if forced { |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 268 | check_key_permission(KeyPerm::ReqForcedOp, k, &av)?; |
Janis Danisevskis | 186d9f4 | 2021-03-03 14:40:52 -0800 | [diff] [blame] | 269 | } |
| 270 | Ok(()) |
| 271 | }, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 272 | ) |
| 273 | }) |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 274 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 275 | .context(ks_err!("Failed to load key blob."))?; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 276 | |
| 277 | let (blob, blob_metadata) = |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 278 | key_entry.take_key_blob_info().ok_or_else(Error::sys).context(ks_err!( |
| 279 | "Successfully loaded key entry, \ |
| 280 | but KM blob was missing." |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 281 | ))?; |
| 282 | scoping_blob = blob; |
| 283 | |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame] | 284 | ( |
| 285 | &scoping_blob, |
| 286 | Some((key_id_guard.id(), key_entry.into_key_parameters())), |
| 287 | Some(key_id_guard), |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 288 | blob_metadata, |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame] | 289 | ) |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 290 | } |
| 291 | }; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 292 | |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 293 | 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] | 294 | Err(Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 295 | .context(ks_err!("No operation purpose specified.")), |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame] | 296 | |kp| match kp.value { |
| 297 | KeyParameterValue::KeyPurpose(p) => Ok(p), |
| 298 | _ => Err(Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 299 | .context(ks_err!("Malformed KeyParameter.")), |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame] | 300 | }, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 301 | )?; |
| 302 | |
Satya Tangirala | 2642ff9 | 2021-04-15 01:57:00 -0700 | [diff] [blame] | 303 | // Remove Tag::PURPOSE from the operation_parameters, since some keymaster devices return |
| 304 | // an error on begin() if Tag::PURPOSE is in the operation_parameters. |
| 305 | let op_params: Vec<KeyParameter> = |
| 306 | operation_parameters.iter().filter(|p| p.tag != Tag::PURPOSE).cloned().collect(); |
| 307 | let operation_parameters = op_params.as_slice(); |
| 308 | |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 309 | let (immediate_hat, mut auth_info) = ENFORCEMENTS |
| 310 | .authorize_create( |
| 311 | purpose, |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame] | 312 | key_properties.as_ref(), |
| 313 | operation_parameters.as_ref(), |
Janis Danisevskis | e3f7d20 | 2021-03-20 14:21:22 -0700 | [diff] [blame] | 314 | self.hw_info.timestampTokenRequired, |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 315 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 316 | .context(ks_err!())?; |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 317 | |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 318 | let km_blob = SUPER_KEY |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 319 | .read() |
| 320 | .unwrap() |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 321 | .unwrap_key_if_required(&blob_metadata, km_blob) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 322 | .context(ks_err!("Failed to handle super encryption."))?; |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 323 | |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 324 | let (begin_result, upgraded_blob) = self |
| 325 | .upgrade_keyblob_if_required_with( |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 326 | key_id_guard, |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 327 | &km_blob, |
Max Bires | 55620ff | 2022-02-11 13:34:15 -0800 | [diff] [blame] | 328 | blob_metadata.km_uuid().copied(), |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 329 | operation_parameters, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 330 | |blob| loop { |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 331 | match map_km_error({ |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 332 | let _wp = |
| 333 | self.watch("In KeystoreSecurityLevel::create_operation: calling begin"); |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 334 | self.keymint.begin( |
| 335 | purpose, |
| 336 | blob, |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 337 | operation_parameters, |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 338 | immediate_hat.as_ref(), |
| 339 | ) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 340 | }) { |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 341 | Err(Error::Km(ErrorCode::TOO_MANY_OPERATIONS)) => { |
Janis Danisevskis | 186d9f4 | 2021-03-03 14:40:52 -0800 | [diff] [blame] | 342 | self.operation_db.prune(caller_uid, forced)?; |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 343 | continue; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 344 | } |
Pavel Grafov | f45034a | 2021-05-12 22:35:45 +0100 | [diff] [blame] | 345 | v @ Err(Error::Km(ErrorCode::INVALID_KEY_BLOB)) => { |
| 346 | if let Some((key_id, _)) = key_properties { |
| 347 | if let Ok(Some(key)) = |
| 348 | DB.with(|db| db.borrow_mut().load_key_descriptor(key_id)) |
| 349 | { |
| 350 | log_key_integrity_violation(&key); |
| 351 | } else { |
| 352 | log::error!("Failed to load key descriptor for audit log"); |
| 353 | } |
| 354 | } |
| 355 | return v; |
| 356 | } |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 357 | v => return v, |
| 358 | } |
| 359 | }, |
| 360 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 361 | .context(ks_err!("Failed to begin operation."))?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 362 | |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 363 | let operation_challenge = auth_info.finalize_create_authorization(begin_result.challenge); |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 364 | |
Hasini Gunasinghe | 0aba68a | 2021-03-19 00:43:52 +0000 | [diff] [blame] | 365 | let op_params: Vec<KeyParameter> = operation_parameters.to_vec(); |
| 366 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 367 | let operation = match begin_result.operation { |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 368 | Some(km_op) => self.operation_db.create_operation( |
| 369 | km_op, |
| 370 | caller_uid, |
| 371 | auth_info, |
| 372 | forced, |
| 373 | LoggingInfo::new(self.security_level, purpose, op_params, upgraded_blob.is_some()), |
| 374 | ), |
| 375 | None => { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 376 | return Err(Error::sys()).context(ks_err!( |
| 377 | "Begin operation returned successfully, \ |
| 378 | but did not return a valid operation." |
| 379 | )); |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 380 | } |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 381 | }; |
| 382 | |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 383 | let op_binder: binder::Strong<dyn IKeystoreOperation> = |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 384 | KeystoreOperation::new_native_binder(operation) |
| 385 | .as_binder() |
| 386 | .into_interface() |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 387 | .context(ks_err!("Failed to create IKeystoreOperation."))?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 388 | |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 389 | Ok(CreateOperationResponse { |
| 390 | iOperation: Some(op_binder), |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 391 | operationChallenge: operation_challenge, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 392 | parameters: match begin_result.params.len() { |
| 393 | 0 => None, |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 394 | _ => Some(KeyParameters { keyParameter: begin_result.params }), |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 395 | }, |
Satya Tangirala | e2016a8 | 2021-03-05 09:28:30 -0800 | [diff] [blame] | 396 | // An upgraded blob should only be returned if the caller has permission |
| 397 | // to use Domain::BLOB keys. If we got to this point, we already checked |
| 398 | // that the caller had that permission. |
| 399 | upgradedBlob: if key.domain == Domain::BLOB { upgraded_blob } else { None }, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 400 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 403 | fn add_required_parameters( |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 404 | &self, |
Janis Danisevskis | e766edc | 2021-02-06 12:16:26 -0800 | [diff] [blame] | 405 | uid: u32, |
| 406 | params: &[KeyParameter], |
| 407 | key: &KeyDescriptor, |
| 408 | ) -> Result<Vec<KeyParameter>> { |
Janis Danisevskis | 212c68b | 2021-01-14 22:29:28 -0800 | [diff] [blame] | 409 | let mut result = params.to_vec(); |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 410 | |
Tri Vo | 74997ed | 2023-07-20 17:57:19 -0400 | [diff] [blame] | 411 | // Prevent callers from specifying the CREATION_DATETIME tag. |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 412 | if params.iter().any(|kp| kp.tag == Tag::CREATION_DATETIME) { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 413 | return Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)).context(ks_err!( |
| 414 | "KeystoreSecurityLevel::add_required_parameters: \ |
| 415 | Specifying Tag::CREATION_DATETIME is not allowed." |
| 416 | )); |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Tri Vo | 74997ed | 2023-07-20 17:57:19 -0400 | [diff] [blame] | 419 | // Use this variable to refer to notion of "now". This eliminates discrepancies from |
| 420 | // quering the clock multiple times. |
| 421 | let creation_datetime = SystemTime::now(); |
| 422 | |
Janis Danisevskis | 2b3c723 | 2021-12-20 13:16:23 -0800 | [diff] [blame] | 423 | // Add CREATION_DATETIME only if the backend version Keymint V1 (100) or newer. |
| 424 | if self.hw_info.versionNumber >= 100 { |
| 425 | result.push(KeyParameter { |
| 426 | tag: Tag::CREATION_DATETIME, |
| 427 | value: KeyParameterValue::DateTime( |
Tri Vo | 74997ed | 2023-07-20 17:57:19 -0400 | [diff] [blame] | 428 | creation_datetime |
Janis Danisevskis | 2b3c723 | 2021-12-20 13:16:23 -0800 | [diff] [blame] | 429 | .duration_since(SystemTime::UNIX_EPOCH) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 430 | .context(ks_err!( |
| 431 | "KeystoreSecurityLevel::add_required_parameters: \ |
| 432 | Failed to get epoch time." |
| 433 | ))? |
Janis Danisevskis | 2b3c723 | 2021-12-20 13:16:23 -0800 | [diff] [blame] | 434 | .as_millis() |
| 435 | .try_into() |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 436 | .context(ks_err!( |
| 437 | "KeystoreSecurityLevel::add_required_parameters: \ |
| 438 | Failed to convert epoch time." |
| 439 | ))?, |
Janis Danisevskis | 2b3c723 | 2021-12-20 13:16:23 -0800 | [diff] [blame] | 440 | ), |
| 441 | }); |
| 442 | } |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 443 | |
Janis Danisevskis | 2c08401 | 2021-01-31 22:23:17 -0800 | [diff] [blame] | 444 | // 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] | 445 | if params.iter().any(|kp| kp.tag == Tag::ATTESTATION_CHALLENGE) { |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 446 | let aaid = { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 447 | let _wp = self |
| 448 | .watch("In KeystoreSecurityLevel::add_required_parameters calling: get_aaid"); |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 449 | keystore2_aaid::get_aaid(uid) |
| 450 | .map_err(|e| anyhow!(ks_err!("get_aaid returned status {}.", e))) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 451 | }?; |
| 452 | |
Janis Danisevskis | 212c68b | 2021-01-14 22:29:28 -0800 | [diff] [blame] | 453 | result.push(KeyParameter { |
| 454 | tag: Tag::ATTESTATION_APPLICATION_ID, |
| 455 | value: KeyParameterValue::Blob(aaid), |
| 456 | }); |
| 457 | } |
Janis Danisevskis | 2c08401 | 2021-01-31 22:23:17 -0800 | [diff] [blame] | 458 | |
Janis Danisevskis | e766edc | 2021-02-06 12:16:26 -0800 | [diff] [blame] | 459 | if params.iter().any(|kp| kp.tag == Tag::INCLUDE_UNIQUE_ID) { |
Seth Moore | 66d9e90 | 2022-03-16 17:20:31 -0700 | [diff] [blame] | 460 | if check_key_permission(KeyPerm::GenUniqueId, key, &None).is_err() |
| 461 | && check_unique_id_attestation_permissions().is_err() |
| 462 | { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 463 | return Err(Error::perm()).context(ks_err!( |
| 464 | "Caller does not have the permission to generate a unique ID" |
| 465 | )); |
Seth Moore | 66d9e90 | 2022-03-16 17:20:31 -0700 | [diff] [blame] | 466 | } |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 467 | if self |
| 468 | .id_rotation_state |
Tri Vo | 74997ed | 2023-07-20 17:57:19 -0400 | [diff] [blame] | 469 | .had_factory_reset_since_id_rotation(&creation_datetime) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 470 | .context(ks_err!("Call to had_factory_reset_since_id_rotation failed."))? |
| 471 | { |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 472 | result.push(KeyParameter { |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 473 | tag: Tag::RESET_SINCE_ID_ROTATION, |
| 474 | value: KeyParameterValue::BoolValue(true), |
| 475 | }) |
| 476 | } |
Janis Danisevskis | e766edc | 2021-02-06 12:16:26 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 479 | // If the caller requests any device identifier attestation tag, check that they hold the |
| 480 | // correct Android permission. |
| 481 | if params.iter().any(|kp| is_device_id_attestation_tag(kp.tag)) { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 482 | check_device_attestation_permissions().context(ks_err!( |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 483 | "Caller does not have the permission to attest device identifiers." |
| 484 | ))?; |
| 485 | } |
| 486 | |
Janis Danisevskis | 2c08401 | 2021-01-31 22:23:17 -0800 | [diff] [blame] | 487 | // If we are generating/importing an asymmetric key, we need to make sure |
| 488 | // that NOT_BEFORE and NOT_AFTER are present. |
| 489 | match params.iter().find(|kp| kp.tag == Tag::ALGORITHM) { |
| 490 | Some(KeyParameter { tag: _, value: KeyParameterValue::Algorithm(Algorithm::RSA) }) |
| 491 | | Some(KeyParameter { tag: _, value: KeyParameterValue::Algorithm(Algorithm::EC) }) => { |
| 492 | if !params.iter().any(|kp| kp.tag == Tag::CERTIFICATE_NOT_BEFORE) { |
| 493 | result.push(KeyParameter { |
| 494 | tag: Tag::CERTIFICATE_NOT_BEFORE, |
| 495 | value: KeyParameterValue::DateTime(0), |
| 496 | }) |
| 497 | } |
| 498 | if !params.iter().any(|kp| kp.tag == Tag::CERTIFICATE_NOT_AFTER) { |
| 499 | result.push(KeyParameter { |
| 500 | tag: Tag::CERTIFICATE_NOT_AFTER, |
| 501 | value: KeyParameterValue::DateTime(UNDEFINED_NOT_AFTER), |
| 502 | }) |
| 503 | } |
| 504 | } |
| 505 | _ => {} |
| 506 | } |
Janis Danisevskis | 212c68b | 2021-01-14 22:29:28 -0800 | [diff] [blame] | 507 | Ok(result) |
| 508 | } |
| 509 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 510 | fn generate_key( |
| 511 | &self, |
| 512 | key: &KeyDescriptor, |
Shawn Willden | 8fde4c2 | 2021-02-14 13:58:22 -0700 | [diff] [blame] | 513 | attest_key_descriptor: Option<&KeyDescriptor>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 514 | params: &[KeyParameter], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 515 | flags: i32, |
Paul Crowley | d5653e5 | 2021-03-25 09:46:31 -0700 | [diff] [blame] | 516 | _entropy: &[u8], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 517 | ) -> Result<KeyMetadata> { |
Shaquille Johnson | 9a587a2 | 2024-02-28 19:54:24 +0000 | [diff] [blame] | 518 | log::info!("security_level: generate_key(key={:?})", key); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 519 | if key.domain != Domain::BLOB && key.alias.is_none() { |
| 520 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 521 | .context(ks_err!("Alias must be specified")); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 522 | } |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 523 | let caller_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 524 | |
| 525 | let key = match key.domain { |
| 526 | Domain::APP => KeyDescriptor { |
| 527 | domain: key.domain, |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 528 | nspace: caller_uid as i64, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 529 | alias: key.alias.clone(), |
| 530 | blob: None, |
| 531 | }, |
| 532 | _ => key.clone(), |
| 533 | }; |
| 534 | |
| 535 | // generate_key requires the rebind permission. |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 536 | // Must return on error for security reasons. |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 537 | check_key_permission(KeyPerm::Rebind, &key, &None).context(ks_err!())?; |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 538 | |
| 539 | let attestation_key_info = match (key.domain, attest_key_descriptor) { |
| 540 | (Domain::BLOB, _) => None, |
Satya Tangirala | fdb9c76 | 2021-03-09 22:34:22 -0800 | [diff] [blame] | 541 | _ => DB |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 542 | .with(|db| { |
| 543 | get_attest_key_info( |
Satya Tangirala | fdb9c76 | 2021-03-09 22:34:22 -0800 | [diff] [blame] | 544 | &key, |
| 545 | caller_uid, |
| 546 | attest_key_descriptor, |
| 547 | params, |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 548 | &self.rem_prov_state, |
Satya Tangirala | fdb9c76 | 2021-03-09 22:34:22 -0800 | [diff] [blame] | 549 | &mut db.borrow_mut(), |
| 550 | ) |
| 551 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 552 | .context(ks_err!("Trying to get an attestation key"))?, |
Satya Tangirala | fdb9c76 | 2021-03-09 22:34:22 -0800 | [diff] [blame] | 553 | }; |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 554 | let params = self |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 555 | .add_required_parameters(caller_uid, params, &key) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 556 | .context(ks_err!("Trying to get aaid."))?; |
Janis Danisevskis | 212c68b | 2021-01-14 22:29:28 -0800 | [diff] [blame] | 557 | |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 558 | let creation_result = match attestation_key_info { |
| 559 | Some(AttestationKeyInfo::UserGenerated { |
| 560 | key_id_guard, |
| 561 | blob, |
| 562 | blob_metadata, |
| 563 | issuer_subject, |
| 564 | }) => self |
| 565 | .upgrade_keyblob_if_required_with( |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 566 | Some(key_id_guard), |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 567 | &KeyBlob::Ref(&blob), |
Max Bires | 55620ff | 2022-02-11 13:34:15 -0800 | [diff] [blame] | 568 | blob_metadata.km_uuid().copied(), |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 569 | ¶ms, |
| 570 | |blob| { |
| 571 | let attest_key = Some(AttestationKey { |
| 572 | keyBlob: blob.to_vec(), |
| 573 | attestKeyParams: vec![], |
| 574 | issuerSubjectName: issuer_subject.clone(), |
| 575 | }); |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 576 | map_km_error({ |
| 577 | let _wp = self.watch_millis( |
| 578 | concat!( |
| 579 | "In KeystoreSecurityLevel::generate_key (UserGenerated): ", |
| 580 | "calling generate_key." |
| 581 | ), |
| 582 | 5000, // Generate can take a little longer. |
| 583 | ); |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 584 | self.keymint.generateKey(¶ms, attest_key.as_ref()) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 585 | }) |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 586 | }, |
| 587 | ) |
Shaquille Johnson | 9a587a2 | 2024-02-28 19:54:24 +0000 | [diff] [blame] | 588 | .context(ks_err!( |
| 589 | "While generating Key {:?} with remote \ |
| 590 | provisioned attestation key and params: {:?}.", |
| 591 | key.alias, |
| 592 | log_security_safe_params(¶ms) |
| 593 | )) |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 594 | .map(|(result, _)| result), |
Tri Vo | b5e43d1 | 2022-12-21 08:54:14 -0800 | [diff] [blame] | 595 | Some(AttestationKeyInfo::RkpdProvisioned { attestation_key, attestation_certs }) => { |
| 596 | self.upgrade_rkpd_keyblob_if_required_with(&attestation_key.keyBlob, &[], |blob| { |
| 597 | map_km_error({ |
| 598 | let _wp = self.watch_millis( |
| 599 | concat!( |
| 600 | "In KeystoreSecurityLevel::generate_key (RkpdProvisioned): ", |
| 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 | }) |
Shaquille Johnson | 9a587a2 | 2024-02-28 19:54:24 +0000 | [diff] [blame] | 613 | .context(ks_err!( |
| 614 | "While generating Key {:?} with remote \ |
| 615 | provisioned attestation key and params: {:?}.", |
| 616 | key.alias, |
| 617 | log_security_safe_params(¶ms) |
| 618 | )) |
Tri Vo | b5e43d1 | 2022-12-21 08:54:14 -0800 | [diff] [blame] | 619 | .map(|(mut result, _)| { |
| 620 | result.certificateChain.push(attestation_certs); |
| 621 | result |
| 622 | }) |
| 623 | } |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 624 | None => map_km_error({ |
| 625 | let _wp = self.watch_millis( |
| 626 | concat!( |
| 627 | "In KeystoreSecurityLevel::generate_key (No attestation): ", |
| 628 | "calling generate_key.", |
| 629 | ), |
| 630 | 5000, // Generate can take a little longer. |
| 631 | ); |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 632 | self.keymint.generateKey(¶ms, None) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 633 | }) |
Shaquille Johnson | 9a587a2 | 2024-02-28 19:54:24 +0000 | [diff] [blame] | 634 | .context(ks_err!( |
| 635 | "While generating Key {:?} with remote \ |
| 636 | provisioned attestation key and params: {:?}.", |
| 637 | key.alias, |
| 638 | log_security_safe_params(¶ms) |
| 639 | )), |
Max Bires | 97f9681 | 2021-02-23 23:44:57 -0800 | [diff] [blame] | 640 | } |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 641 | .context(ks_err!())?; |
Janis Danisevskis | 3541f3e | 2021-03-20 14:18:52 -0700 | [diff] [blame] | 642 | |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 643 | let user_id = uid_to_android_user(caller_uid); |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 644 | 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] | 645 | } |
| 646 | |
| 647 | fn import_key( |
| 648 | &self, |
| 649 | key: &KeyDescriptor, |
Paul Crowley | d5653e5 | 2021-03-25 09:46:31 -0700 | [diff] [blame] | 650 | _attestation_key: Option<&KeyDescriptor>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 651 | params: &[KeyParameter], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 652 | flags: i32, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 653 | key_data: &[u8], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 654 | ) -> Result<KeyMetadata> { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 655 | if key.domain != Domain::BLOB && key.alias.is_none() { |
| 656 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 657 | .context(ks_err!("Alias must be specified")); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 658 | } |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 659 | let caller_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 660 | |
| 661 | let key = match key.domain { |
| 662 | Domain::APP => KeyDescriptor { |
| 663 | domain: key.domain, |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 664 | nspace: caller_uid as i64, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 665 | alias: key.alias.clone(), |
| 666 | blob: None, |
| 667 | }, |
| 668 | _ => key.clone(), |
| 669 | }; |
| 670 | |
| 671 | // import_key requires the rebind permission. |
Shaquille Johnson | e8b152a | 2023-02-09 15:15:50 +0000 | [diff] [blame] | 672 | check_key_permission(KeyPerm::Rebind, &key, &None).context(ks_err!("In import_key."))?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 673 | |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 674 | let params = self |
Janis Danisevskis | d43c1b9 | 2021-11-09 14:56:17 +0000 | [diff] [blame] | 675 | .add_required_parameters(caller_uid, params, &key) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 676 | .context(ks_err!("Trying to get aaid."))?; |
Janis Danisevskis | 212c68b | 2021-01-14 22:29:28 -0800 | [diff] [blame] | 677 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 678 | let format = params |
| 679 | .iter() |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 680 | .find(|p| p.tag == Tag::ALGORITHM) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 681 | .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | e8b152a | 2023-02-09 15:15:50 +0000 | [diff] [blame] | 682 | .context(ks_err!("No KeyParameter 'Algorithm'.")) |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame] | 683 | .and_then(|p| match &p.value { |
| 684 | KeyParameterValue::Algorithm(Algorithm::AES) |
| 685 | | KeyParameterValue::Algorithm(Algorithm::HMAC) |
| 686 | | KeyParameterValue::Algorithm(Algorithm::TRIPLE_DES) => Ok(KeyFormat::RAW), |
| 687 | KeyParameterValue::Algorithm(Algorithm::RSA) |
| 688 | | KeyParameterValue::Algorithm(Algorithm::EC) => Ok(KeyFormat::PKCS8), |
| 689 | v => Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | aec2eca | 2022-11-30 17:08:05 +0000 | [diff] [blame] | 690 | .context(ks_err!("Unknown Algorithm {:?}.", v)), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 691 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 692 | .context(ks_err!())?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 693 | |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 694 | let km_dev = &self.keymint; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 695 | let creation_result = map_km_error({ |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 696 | let _wp = self.watch("In KeystoreSecurityLevel::import_key: calling importKey."); |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 697 | km_dev.importKey(¶ms, format, key_data, None /* attestKey */) |
| 698 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 699 | .context(ks_err!("Trying to call importKey"))?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 700 | |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 701 | let user_id = uid_to_android_user(caller_uid); |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 702 | 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] | 703 | } |
| 704 | |
| 705 | fn import_wrapped_key( |
| 706 | &self, |
| 707 | key: &KeyDescriptor, |
| 708 | wrapping_key: &KeyDescriptor, |
| 709 | masking_key: Option<&[u8]>, |
| 710 | params: &[KeyParameter], |
| 711 | authenticators: &[AuthenticatorSpec], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 712 | ) -> Result<KeyMetadata> { |
Janis Danisevskis | 32adc7d | 2021-02-07 14:04:01 -0800 | [diff] [blame] | 713 | let wrapped_data: &[u8] = match key { |
| 714 | KeyDescriptor { domain: Domain::APP, blob: Some(ref blob), alias: Some(_), .. } |
| 715 | | KeyDescriptor { |
| 716 | domain: Domain::SELINUX, blob: Some(ref blob), alias: Some(_), .. |
| 717 | } => blob, |
| 718 | _ => { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 719 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)).context(ks_err!( |
| 720 | "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] | 721 | key |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 722 | )); |
Janis Danisevskis | 32adc7d | 2021-02-07 14:04:01 -0800 | [diff] [blame] | 723 | } |
| 724 | }; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 725 | |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 726 | if wrapping_key.domain == Domain::BLOB { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 727 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
| 728 | .context(ks_err!("Import wrapped key not supported for self managed blobs.")); |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 729 | } |
| 730 | |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 731 | let caller_uid = ThreadState::get_calling_uid(); |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 732 | let user_id = uid_to_android_user(caller_uid); |
| 733 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 734 | let key = match key.domain { |
| 735 | Domain::APP => KeyDescriptor { |
| 736 | domain: key.domain, |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 737 | nspace: caller_uid as i64, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 738 | alias: key.alias.clone(), |
| 739 | blob: None, |
| 740 | }, |
Janis Danisevskis | 32adc7d | 2021-02-07 14:04:01 -0800 | [diff] [blame] | 741 | Domain::SELINUX => KeyDescriptor { |
| 742 | domain: Domain::SELINUX, |
| 743 | nspace: key.nspace, |
| 744 | alias: key.alias.clone(), |
| 745 | blob: None, |
| 746 | }, |
| 747 | _ => panic!("Unreachable."), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 748 | }; |
| 749 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 750 | // Import_wrapped_key requires the rebind permission for the new key. |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 751 | check_key_permission(KeyPerm::Rebind, &key, &None).context(ks_err!())?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 752 | |
Eric Biggers | 673d34a | 2023-10-18 01:54:18 +0000 | [diff] [blame] | 753 | let super_key = SUPER_KEY.read().unwrap().get_after_first_unlock_key_by_user_id(user_id); |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 754 | |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 755 | let (wrapping_key_id_guard, mut wrapping_key_entry) = DB |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 756 | .with(|db| { |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 757 | LEGACY_IMPORTER.with_try_import(&key, caller_uid, super_key, || { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 758 | db.borrow_mut().load_key_entry( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 759 | wrapping_key, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 760 | KeyType::Client, |
| 761 | KeyEntryLoadBits::KM, |
| 762 | caller_uid, |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 763 | |k, av| check_key_permission(KeyPerm::Use, k, &av), |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 764 | ) |
| 765 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 766 | }) |
Shaquille Johnson | e8b152a | 2023-02-09 15:15:50 +0000 | [diff] [blame] | 767 | .context(ks_err!("Failed to load wrapping key."))?; |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 768 | |
Shaquille Johnson | aec2eca | 2022-11-30 17:08:05 +0000 | [diff] [blame] | 769 | let (wrapping_key_blob, wrapping_blob_metadata) = |
| 770 | wrapping_key_entry.take_key_blob_info().ok_or_else(error::Error::sys).context( |
| 771 | ks_err!("No km_blob after successfully loading key. This should never happen."), |
| 772 | )?; |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 773 | |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 774 | let wrapping_key_blob = SUPER_KEY |
| 775 | .read() |
| 776 | .unwrap() |
| 777 | .unwrap_key_if_required(&wrapping_blob_metadata, &wrapping_key_blob) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 778 | .context(ks_err!("Failed to handle super encryption for wrapping key."))?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 779 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 780 | // km_dev.importWrappedKey does not return a certificate chain. |
| 781 | // TODO Do we assume that all wrapped keys are symmetric? |
| 782 | // let certificate_chain: Vec<KmCertificate> = Default::default(); |
| 783 | |
| 784 | let pw_sid = authenticators |
| 785 | .iter() |
| 786 | .find_map(|a| match a.authenticatorType { |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 787 | HardwareAuthenticatorType::PASSWORD => Some(a.authenticatorId), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 788 | _ => None, |
| 789 | }) |
Janis Danisevskis | 32adc7d | 2021-02-07 14:04:01 -0800 | [diff] [blame] | 790 | .unwrap_or(-1); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 791 | |
| 792 | let fp_sid = authenticators |
| 793 | .iter() |
| 794 | .find_map(|a| match a.authenticatorType { |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 795 | HardwareAuthenticatorType::FINGERPRINT => Some(a.authenticatorId), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 796 | _ => None, |
| 797 | }) |
Janis Danisevskis | 32adc7d | 2021-02-07 14:04:01 -0800 | [diff] [blame] | 798 | .unwrap_or(-1); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 799 | |
| 800 | let masking_key = masking_key.unwrap_or(ZERO_BLOB_32); |
| 801 | |
Janis Danisevskis | 104d8e4 | 2021-01-14 22:49:27 -0800 | [diff] [blame] | 802 | let (creation_result, _) = self |
| 803 | .upgrade_keyblob_if_required_with( |
Janis Danisevskis | 104d8e4 | 2021-01-14 22:49:27 -0800 | [diff] [blame] | 804 | Some(wrapping_key_id_guard), |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 805 | &wrapping_key_blob, |
Max Bires | 55620ff | 2022-02-11 13:34:15 -0800 | [diff] [blame] | 806 | wrapping_blob_metadata.km_uuid().copied(), |
Janis Danisevskis | 104d8e4 | 2021-01-14 22:49:27 -0800 | [diff] [blame] | 807 | &[], |
| 808 | |wrapping_blob| { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 809 | let _wp = self.watch( |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 810 | "In KeystoreSecurityLevel::import_wrapped_key: calling importWrappedKey.", |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 811 | ); |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 812 | let creation_result = map_km_error(self.keymint.importWrappedKey( |
Janis Danisevskis | 104d8e4 | 2021-01-14 22:49:27 -0800 | [diff] [blame] | 813 | wrapped_data, |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 814 | wrapping_blob, |
Janis Danisevskis | 104d8e4 | 2021-01-14 22:49:27 -0800 | [diff] [blame] | 815 | masking_key, |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 816 | params, |
Janis Danisevskis | 104d8e4 | 2021-01-14 22:49:27 -0800 | [diff] [blame] | 817 | pw_sid, |
| 818 | fp_sid, |
| 819 | ))?; |
| 820 | Ok(creation_result) |
| 821 | }, |
| 822 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 823 | .context(ks_err!())?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 824 | |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 825 | self.store_new_key(key, creation_result, user_id, None) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 826 | .context(ks_err!("Trying to store the new key.")) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 827 | } |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 828 | |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 829 | fn store_upgraded_keyblob( |
| 830 | key_id_guard: KeyIdGuard, |
Max Bires | 55620ff | 2022-02-11 13:34:15 -0800 | [diff] [blame] | 831 | km_uuid: Option<Uuid>, |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 832 | key_blob: &KeyBlob, |
| 833 | upgraded_blob: &[u8], |
| 834 | ) -> Result<()> { |
| 835 | let (upgraded_blob_to_be_stored, new_blob_metadata) = |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 836 | SuperKeyManager::reencrypt_if_required(key_blob, upgraded_blob) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 837 | .context(ks_err!("Failed to handle super encryption."))?; |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 838 | |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 839 | let mut new_blob_metadata = new_blob_metadata.unwrap_or_default(); |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 840 | if let Some(uuid) = km_uuid { |
Max Bires | 55620ff | 2022-02-11 13:34:15 -0800 | [diff] [blame] | 841 | new_blob_metadata.add(BlobMetaEntry::KmUuid(uuid)); |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | DB.with(|db| { |
| 845 | let mut db = db.borrow_mut(); |
| 846 | db.set_blob( |
| 847 | &key_id_guard, |
| 848 | SubComponentType::KEY_BLOB, |
| 849 | Some(&upgraded_blob_to_be_stored), |
| 850 | Some(&new_blob_metadata), |
| 851 | ) |
| 852 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 853 | .context(ks_err!("Failed to insert upgraded blob into the database.")) |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 856 | fn upgrade_keyblob_if_required_with<T, F>( |
| 857 | &self, |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 858 | mut key_id_guard: Option<KeyIdGuard>, |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 859 | key_blob: &KeyBlob, |
Max Bires | 55620ff | 2022-02-11 13:34:15 -0800 | [diff] [blame] | 860 | km_uuid: Option<Uuid>, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 861 | params: &[KeyParameter], |
| 862 | f: F, |
| 863 | ) -> Result<(T, Option<Vec<u8>>)> |
| 864 | where |
| 865 | F: Fn(&[u8]) -> Result<T, Error>, |
| 866 | { |
Shaquille Johnson | 9a587a2 | 2024-02-28 19:54:24 +0000 | [diff] [blame] | 867 | log::info!("upgrade_keyblob_if_required_with(key_id={:?})", key_id_guard); |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 868 | let (v, upgraded_blob) = crate::utils::upgrade_keyblob_if_required_with( |
David Drysdale | 5accbaa | 2023-04-12 18:47:10 +0100 | [diff] [blame] | 869 | &*self.keymint, |
| 870 | self.hw_info.versionNumber, |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 871 | key_blob, |
| 872 | params, |
| 873 | f, |
| 874 | |upgraded_blob| { |
| 875 | if key_id_guard.is_some() { |
| 876 | // Unwrap cannot panic, because the is_some was true. |
| 877 | let kid = key_id_guard.take().unwrap(); |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 878 | Self::store_upgraded_keyblob(kid, km_uuid, key_blob, upgraded_blob) |
| 879 | .context(ks_err!("store_upgraded_keyblob failed")) |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 880 | } else { |
| 881 | Ok(()) |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 882 | } |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 883 | }, |
| 884 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 885 | .context(ks_err!())?; |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 886 | |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 887 | // If no upgrade was needed, use the opportunity to reencrypt the blob if required |
| 888 | // and if the a key_id_guard is held. Note: key_id_guard can only be Some if no |
| 889 | // upgrade was performed above and if one was given in the first place. |
| 890 | if key_blob.force_reencrypt() { |
| 891 | if let Some(kid) = key_id_guard { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 892 | Self::store_upgraded_keyblob(kid, km_uuid, key_blob, key_blob) |
| 893 | .context(ks_err!("store_upgraded_keyblob failed in forced reencrypt"))?; |
Paul Crowley | 8d5b253 | 2021-03-19 10:53:07 -0700 | [diff] [blame] | 894 | } |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 895 | } |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 896 | Ok((v, upgraded_blob)) |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 897 | } |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 898 | |
Tri Vo | b5e43d1 | 2022-12-21 08:54:14 -0800 | [diff] [blame] | 899 | fn upgrade_rkpd_keyblob_if_required_with<T, F>( |
| 900 | &self, |
| 901 | key_blob: &[u8], |
| 902 | params: &[KeyParameter], |
| 903 | f: F, |
| 904 | ) -> Result<(T, Option<Vec<u8>>)> |
| 905 | where |
| 906 | F: Fn(&[u8]) -> Result<T, Error>, |
| 907 | { |
Shaquille Johnson | 9a587a2 | 2024-02-28 19:54:24 +0000 | [diff] [blame] | 908 | log::info!( |
| 909 | "upgrade_rkpd_keyblob_if_required_with(params={:?})", |
| 910 | log_security_safe_params(params) |
| 911 | ); |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 912 | let rpc_name = get_remotely_provisioned_component_name(&self.security_level) |
| 913 | .context(ks_err!("Trying to get IRPC name."))?; |
Tri Vo | b5e43d1 | 2022-12-21 08:54:14 -0800 | [diff] [blame] | 914 | crate::utils::upgrade_keyblob_if_required_with( |
| 915 | &*self.keymint, |
David Drysdale | 5accbaa | 2023-04-12 18:47:10 +0100 | [diff] [blame] | 916 | self.hw_info.versionNumber, |
Tri Vo | b5e43d1 | 2022-12-21 08:54:14 -0800 | [diff] [blame] | 917 | key_blob, |
| 918 | params, |
| 919 | f, |
| 920 | |upgraded_blob| { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 921 | let _wp = wd::watch("Calling store_rkpd_attestation_key()"); |
Alice Wang | 849cfe4 | 2023-11-10 12:43:36 +0000 | [diff] [blame] | 922 | if let Err(e) = store_rkpd_attestation_key(&rpc_name, key_blob, upgraded_blob) { |
| 923 | Err(wrapped_rkpd_error_to_ks_error(&e)).context(format!("{e:?}")) |
| 924 | } else { |
| 925 | Ok(()) |
| 926 | } |
Tri Vo | b5e43d1 | 2022-12-21 08:54:14 -0800 | [diff] [blame] | 927 | }, |
| 928 | ) |
| 929 | .context(ks_err!()) |
| 930 | } |
| 931 | |
Janis Danisevskis | b2434d0 | 2021-04-20 12:49:27 -0700 | [diff] [blame] | 932 | fn convert_storage_key_to_ephemeral( |
| 933 | &self, |
| 934 | storage_key: &KeyDescriptor, |
| 935 | ) -> Result<EphemeralStorageKeyResponse> { |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 936 | if storage_key.domain != Domain::BLOB { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 937 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
| 938 | .context(ks_err!("Key must be of Domain::BLOB")); |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 939 | } |
| 940 | let key_blob = storage_key |
| 941 | .blob |
| 942 | .as_ref() |
| 943 | .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 944 | .context(ks_err!("No key blob specified"))?; |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 945 | |
| 946 | // convert_storage_key_to_ephemeral requires the associated permission |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 947 | check_key_permission(KeyPerm::ConvertStorageKeyToEphemeral, storage_key, &None) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 948 | .context(ks_err!("Check permission"))?; |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 949 | |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 950 | let km_dev = &self.keymint; |
James Farrell | efe1a2f | 2024-02-28 21:36:47 +0000 | [diff] [blame] | 951 | let res = { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 952 | let _wp = self.watch(concat!( |
| 953 | "In IKeystoreSecurityLevel::convert_storage_key_to_ephemeral: ", |
| 954 | "calling convertStorageKeyToEphemeral (1)" |
| 955 | )); |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 956 | map_km_error(km_dev.convertStorageKeyToEphemeral(key_blob)) |
James Farrell | efe1a2f | 2024-02-28 21:36:47 +0000 | [diff] [blame] | 957 | }; |
| 958 | match res { |
Janis Danisevskis | b2434d0 | 2021-04-20 12:49:27 -0700 | [diff] [blame] | 959 | Ok(result) => { |
| 960 | Ok(EphemeralStorageKeyResponse { ephemeralKey: result, upgradedBlob: None }) |
| 961 | } |
| 962 | Err(error::Error::Km(ErrorCode::KEY_REQUIRES_UPGRADE)) => { |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 963 | let upgraded_blob = { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 964 | let _wp = self.watch("In convert_storage_key_to_ephemeral: calling upgradeKey"); |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 965 | map_km_error(km_dev.upgradeKey(key_blob, &[])) |
| 966 | } |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 967 | .context(ks_err!("Failed to upgrade key blob."))?; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 968 | let ephemeral_key = { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 969 | let _wp = self.watch( |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 970 | "In convert_storage_key_to_ephemeral: calling convertStorageKeyToEphemeral (2)", |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 971 | ); |
Janis Danisevskis | 84af4d1 | 2021-07-22 17:39:15 -0700 | [diff] [blame] | 972 | map_km_error(km_dev.convertStorageKeyToEphemeral(&upgraded_blob)) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 973 | } |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 974 | .context(ks_err!( |
Janis Danisevskis | b2434d0 | 2021-04-20 12:49:27 -0700 | [diff] [blame] | 975 | "Failed to retrieve ephemeral key (after upgrade)." |
| 976 | ))?; |
| 977 | Ok(EphemeralStorageKeyResponse { |
| 978 | ephemeralKey: ephemeral_key, |
| 979 | upgradedBlob: Some(upgraded_blob), |
| 980 | }) |
| 981 | } |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 982 | Err(e) => Err(e).context(ks_err!("Failed to retrieve ephemeral key.")), |
Janis Danisevskis | b2434d0 | 2021-04-20 12:49:27 -0700 | [diff] [blame] | 983 | } |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 984 | } |
Satya Tangirala | 04bca0d | 2021-03-08 22:27:54 -0800 | [diff] [blame] | 985 | |
| 986 | fn delete_key(&self, key: &KeyDescriptor) -> Result<()> { |
| 987 | if key.domain != Domain::BLOB { |
| 988 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 989 | .context(ks_err!("delete_key: Key must be of Domain::BLOB")); |
Satya Tangirala | 04bca0d | 2021-03-08 22:27:54 -0800 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | let key_blob = key |
| 993 | .blob |
| 994 | .as_ref() |
| 995 | .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 996 | .context(ks_err!("delete_key: No key blob specified"))?; |
Satya Tangirala | 04bca0d | 2021-03-08 22:27:54 -0800 | [diff] [blame] | 997 | |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 998 | check_key_permission(KeyPerm::Delete, key, &None) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 999 | .context(ks_err!("delete_key: Checking delete permissions"))?; |
Satya Tangirala | 04bca0d | 2021-03-08 22:27:54 -0800 | [diff] [blame] | 1000 | |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 1001 | let km_dev = &self.keymint; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 1002 | { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1003 | let _wp = self.watch("In KeystoreSecuritylevel::delete_key: calling deleteKey"); |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1004 | 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] | 1005 | } |
Satya Tangirala | 04bca0d | 2021-03-08 22:27:54 -0800 | [diff] [blame] | 1006 | } |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | impl binder::Interface for KeystoreSecurityLevel {} |
| 1010 | |
| 1011 | impl IKeystoreSecurityLevel for KeystoreSecurityLevel { |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 1012 | fn createOperation( |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1013 | &self, |
| 1014 | key: &KeyDescriptor, |
| 1015 | operation_parameters: &[KeyParameter], |
| 1016 | forced: bool, |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1017 | ) -> binder::Result<CreateOperationResponse> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1018 | let _wp = self.watch("IKeystoreSecurityLevel::createOperation"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 1019 | self.create_operation(key, operation_parameters, forced).map_err(into_logged_binder) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1020 | } |
| 1021 | fn generateKey( |
| 1022 | &self, |
| 1023 | key: &KeyDescriptor, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 1024 | attestation_key: Option<&KeyDescriptor>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1025 | params: &[KeyParameter], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 1026 | flags: i32, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1027 | entropy: &[u8], |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1028 | ) -> binder::Result<KeyMetadata> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 1029 | // Duration is set to 5 seconds, because generateKey - especially for RSA keys, takes more |
| 1030 | // time than other operations |
| 1031 | let _wp = self.watch_millis("IKeystoreSecurityLevel::generateKey", 5000); |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 1032 | let result = self.generate_key(key, attestation_key, params, flags, entropy); |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 1033 | log_key_creation_event_stats(self.security_level, params, &result); |
Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 1034 | log_key_generated(key, ThreadState::get_calling_uid(), result.is_ok()); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 1035 | result.map_err(into_logged_binder) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1036 | } |
| 1037 | fn importKey( |
| 1038 | &self, |
| 1039 | key: &KeyDescriptor, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 1040 | attestation_key: Option<&KeyDescriptor>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1041 | params: &[KeyParameter], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 1042 | flags: i32, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1043 | key_data: &[u8], |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1044 | ) -> binder::Result<KeyMetadata> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1045 | let _wp = self.watch("IKeystoreSecurityLevel::importKey"); |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 1046 | let result = self.import_key(key, attestation_key, params, flags, key_data); |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 1047 | log_key_creation_event_stats(self.security_level, params, &result); |
Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 1048 | log_key_imported(key, ThreadState::get_calling_uid(), result.is_ok()); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 1049 | result.map_err(into_logged_binder) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1050 | } |
| 1051 | fn importWrappedKey( |
| 1052 | &self, |
| 1053 | key: &KeyDescriptor, |
| 1054 | wrapping_key: &KeyDescriptor, |
| 1055 | masking_key: Option<&[u8]>, |
| 1056 | params: &[KeyParameter], |
| 1057 | authenticators: &[AuthenticatorSpec], |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1058 | ) -> binder::Result<KeyMetadata> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1059 | let _wp = self.watch("IKeystoreSecurityLevel::importWrappedKey"); |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 1060 | let result = |
| 1061 | self.import_wrapped_key(key, wrapping_key, masking_key, params, authenticators); |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 1062 | log_key_creation_event_stats(self.security_level, params, &result); |
Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 1063 | log_key_imported(key, ThreadState::get_calling_uid(), result.is_ok()); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 1064 | result.map_err(into_logged_binder) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1065 | } |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 1066 | fn convertStorageKeyToEphemeral( |
| 1067 | &self, |
| 1068 | storage_key: &KeyDescriptor, |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1069 | ) -> binder::Result<EphemeralStorageKeyResponse> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1070 | let _wp = self.watch("IKeystoreSecurityLevel::convertStorageKeyToEphemeral"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 1071 | self.convert_storage_key_to_ephemeral(storage_key).map_err(into_logged_binder) |
Satya Tangirala | 3361b61 | 2021-03-08 14:36:11 -0800 | [diff] [blame] | 1072 | } |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 1073 | fn deleteKey(&self, key: &KeyDescriptor) -> binder::Result<()> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1074 | let _wp = self.watch("IKeystoreSecurityLevel::deleteKey"); |
Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 1075 | let result = self.delete_key(key); |
| 1076 | log_key_deleted(key, ThreadState::get_calling_uid(), result.is_ok()); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 1077 | result.map_err(into_logged_binder) |
Satya Tangirala | 04bca0d | 2021-03-08 22:27:54 -0800 | [diff] [blame] | 1078 | } |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1079 | } |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 1080 | |
| 1081 | #[cfg(test)] |
| 1082 | mod tests { |
| 1083 | use super::*; |
| 1084 | use crate::error::map_km_error; |
| 1085 | use crate::globals::get_keymint_device; |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 1086 | use crate::utils::upgrade_keyblob_if_required_with; |
| 1087 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
| 1088 | Algorithm::Algorithm, AttestationKey::AttestationKey, KeyParameter::KeyParameter, |
| 1089 | KeyParameterValue::KeyParameterValue, Tag::Tag, |
| 1090 | }; |
| 1091 | use keystore2_crypto::parse_subject_from_certificate; |
Alice Wang | 01c16b6 | 2023-11-07 14:27:49 +0000 | [diff] [blame] | 1092 | use rkpd_client::get_rkpd_attestation_key; |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 1093 | |
| 1094 | #[test] |
| 1095 | // This is a helper for a manual test. We want to check that after a system upgrade RKPD |
| 1096 | // attestation keys can also be upgraded and stored again with RKPD. The steps are: |
| 1097 | // 1. Run this test and check in stdout that no key upgrade happened. |
| 1098 | // 2. Perform a system upgrade. |
| 1099 | // 3. Run this test and check in stdout that key upgrade did happen. |
| 1100 | // |
| 1101 | // Note that this test must be run with that same UID every time. Running as root, i.e. UID 0, |
| 1102 | // should do the trick. Also, use "--nocapture" flag to get stdout. |
| 1103 | fn test_rkpd_attestation_key_upgrade() { |
| 1104 | binder::ProcessState::start_thread_pool(); |
| 1105 | let security_level = SecurityLevel::TRUSTED_ENVIRONMENT; |
| 1106 | let (keymint, info, _) = get_keymint_device(&security_level).unwrap(); |
| 1107 | let key_id = 0; |
| 1108 | let mut key_upgraded = false; |
| 1109 | |
| 1110 | let rpc_name = get_remotely_provisioned_component_name(&security_level).unwrap(); |
| 1111 | let key = get_rkpd_attestation_key(&rpc_name, key_id).unwrap(); |
| 1112 | assert!(!key.keyBlob.is_empty()); |
| 1113 | assert!(!key.encodedCertChain.is_empty()); |
| 1114 | |
| 1115 | upgrade_keyblob_if_required_with( |
| 1116 | &*keymint, |
| 1117 | info.versionNumber, |
| 1118 | &key.keyBlob, |
| 1119 | /*upgrade_params=*/ &[], |
| 1120 | /*km_op=*/ |
| 1121 | |blob| { |
| 1122 | let params = vec![ |
| 1123 | KeyParameter { |
| 1124 | tag: Tag::ALGORITHM, |
| 1125 | value: KeyParameterValue::Algorithm(Algorithm::AES), |
| 1126 | }, |
| 1127 | KeyParameter { |
| 1128 | tag: Tag::ATTESTATION_CHALLENGE, |
| 1129 | value: KeyParameterValue::Blob(vec![0; 16]), |
| 1130 | }, |
| 1131 | KeyParameter { tag: Tag::KEY_SIZE, value: KeyParameterValue::Integer(128) }, |
| 1132 | ]; |
| 1133 | let attestation_key = AttestationKey { |
| 1134 | keyBlob: blob.to_vec(), |
| 1135 | attestKeyParams: vec![], |
| 1136 | issuerSubjectName: parse_subject_from_certificate(&key.encodedCertChain) |
| 1137 | .unwrap(), |
| 1138 | }; |
| 1139 | |
| 1140 | map_km_error(keymint.generateKey(¶ms, Some(&attestation_key))) |
| 1141 | }, |
| 1142 | /*new_blob_handler=*/ |
| 1143 | |new_blob| { |
| 1144 | // This handler is only executed if a key upgrade was performed. |
| 1145 | key_upgraded = true; |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1146 | let _wp = wd::watch("Calling store_rkpd_attestation_key()"); |
Alice Wang | bf6a693 | 2023-11-07 11:47:12 +0000 | [diff] [blame] | 1147 | store_rkpd_attestation_key(&rpc_name, &key.keyBlob, new_blob).unwrap(); |
| 1148 | Ok(()) |
| 1149 | }, |
| 1150 | ) |
| 1151 | .unwrap(); |
| 1152 | |
| 1153 | if key_upgraded { |
| 1154 | println!("RKPD key was upgraded and stored with RKPD."); |
| 1155 | } else { |
| 1156 | println!("RKPD key was NOT upgraded."); |
| 1157 | } |
| 1158 | } |
| 1159 | } |