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