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