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 | |
| 15 | #![allow(unused_variables)] |
| 16 | |
| 17 | //! This crate implements the IKeystoreSecurityLevel interface. |
| 18 | |
Janis Danisevskis | 4507f3b | 2021-01-13 16:34:39 -0800 | [diff] [blame] | 19 | use crate::gc::Gc; |
Shawn Willden | 708744a | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 20 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 21 | Algorithm::Algorithm, HardwareAuthenticatorType::HardwareAuthenticatorType, |
| 22 | IKeyMintDevice::IKeyMintDevice, KeyCreationResult::KeyCreationResult, KeyFormat::KeyFormat, |
| 23 | KeyParameter::KeyParameter, KeyParameterValue::KeyParameterValue, SecurityLevel::SecurityLevel, |
| 24 | Tag::Tag, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 25 | }; |
| 26 | use android_system_keystore2::aidl::android::system::keystore2::{ |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 27 | AuthenticatorSpec::AuthenticatorSpec, CreateOperationResponse::CreateOperationResponse, |
| 28 | Domain::Domain, IKeystoreOperation::IKeystoreOperation, |
| 29 | IKeystoreSecurityLevel::BnKeystoreSecurityLevel, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 30 | IKeystoreSecurityLevel::IKeystoreSecurityLevel, KeyDescriptor::KeyDescriptor, |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 31 | KeyMetadata::KeyMetadata, KeyParameters::KeyParameters, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 34 | use crate::globals::ENFORCEMENTS; |
| 35 | use crate::key_parameter::KeyParameter as KsKeyParam; |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 36 | use crate::key_parameter::KeyParameterValue as KsKeyParamValue; |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame^] | 37 | use crate::utils::{check_key_permission, uid_to_android_user, Asp}; |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 38 | use crate::{database::KeyIdGuard, globals::DB}; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 39 | use crate::{ |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 40 | database::{DateTime, KeyMetaData, KeyMetaEntry, KeyType}, |
| 41 | permission::KeyPerm, |
| 42 | }; |
| 43 | use crate::{ |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 44 | database::{KeyEntry, KeyEntryLoadBits, SubComponentType}, |
| 45 | operation::KeystoreOperation, |
| 46 | operation::OperationDb, |
| 47 | }; |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 48 | use crate::{ |
| 49 | error::{self, map_km_error, map_or_log_err, Error, ErrorCode}, |
| 50 | utils::key_characteristics_to_internal, |
| 51 | }; |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 52 | use anyhow::{Context, Result}; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 53 | use binder::{IBinder, Interface, ThreadState}; |
| 54 | |
| 55 | /// Implementation of the IKeystoreSecurityLevel Interface. |
| 56 | pub struct KeystoreSecurityLevel { |
| 57 | security_level: SecurityLevel, |
| 58 | keymint: Asp, |
| 59 | operation_db: OperationDb, |
| 60 | } |
| 61 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 62 | // Blob of 32 zeroes used as empty masking key. |
| 63 | static ZERO_BLOB_32: &[u8] = &[0; 32]; |
| 64 | |
| 65 | impl KeystoreSecurityLevel { |
| 66 | /// Creates a new security level instance wrapped in a |
| 67 | /// BnKeystoreSecurityLevel proxy object. It also |
| 68 | /// calls `IBinder::set_requesting_sid` on the new interface, because |
| 69 | /// we need it for checking keystore permissions. |
| 70 | pub fn new_native_binder( |
| 71 | security_level: SecurityLevel, |
| 72 | ) -> Result<impl IKeystoreSecurityLevel + Send> { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 73 | let result = BnKeystoreSecurityLevel::new_binder(Self { |
| 74 | security_level, |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 75 | keymint: crate::globals::get_keymint_device(security_level) |
| 76 | .context("In KeystoreSecurityLevel::new_native_binder.")?, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 77 | operation_db: OperationDb::new(), |
| 78 | }); |
| 79 | result.as_binder().set_requesting_sid(true); |
| 80 | Ok(result) |
| 81 | } |
| 82 | |
| 83 | fn store_new_key( |
| 84 | &self, |
| 85 | key: KeyDescriptor, |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 86 | creation_result: KeyCreationResult, |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 87 | user_id: u32, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 88 | ) -> Result<KeyMetadata> { |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 89 | let KeyCreationResult { |
| 90 | keyBlob: key_blob, |
| 91 | keyCharacteristics: key_characteristics, |
| 92 | certificateChain: mut certificate_chain, |
| 93 | } = creation_result; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 94 | |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 95 | let (cert, cert_chain): (Option<Vec<u8>>, Option<Vec<u8>>) = ( |
| 96 | match certificate_chain.len() { |
| 97 | 0 => None, |
| 98 | _ => Some(certificate_chain.remove(0).encodedCertificate), |
| 99 | }, |
| 100 | match certificate_chain.len() { |
| 101 | 0 => None, |
| 102 | _ => Some( |
| 103 | certificate_chain |
| 104 | .iter() |
| 105 | .map(|c| c.encodedCertificate.iter()) |
| 106 | .flatten() |
| 107 | .copied() |
| 108 | .collect(), |
| 109 | ), |
| 110 | }, |
| 111 | ); |
| 112 | |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 113 | let mut key_parameters = key_characteristics_to_internal(key_characteristics); |
| 114 | |
| 115 | key_parameters.push(KsKeyParam::new( |
| 116 | KsKeyParamValue::UserID(user_id as i32), |
| 117 | SecurityLevel::SOFTWARE, |
| 118 | )); |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 119 | |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 120 | let creation_date = DateTime::now().context("Trying to make creation time.")?; |
| 121 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 122 | let key = match key.domain { |
| 123 | Domain::BLOB => { |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 124 | KeyDescriptor { domain: Domain::BLOB, blob: Some(key_blob), ..Default::default() } |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 125 | } |
| 126 | _ => DB |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 127 | .with::<_, Result<KeyDescriptor>>(|db| { |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 128 | let mut metadata = KeyMetaData::new(); |
| 129 | metadata.add(KeyMetaEntry::CreationDate(creation_date)); |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 130 | |
| 131 | let mut db = db.borrow_mut(); |
Janis Danisevskis | 4507f3b | 2021-01-13 16:34:39 -0800 | [diff] [blame] | 132 | let (need_gc, key_id) = db |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 133 | .store_new_key( |
| 134 | key, |
| 135 | &key_parameters, |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 136 | &key_blob, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 137 | cert.as_deref(), |
| 138 | cert_chain.as_deref(), |
| 139 | &metadata, |
| 140 | ) |
| 141 | .context("In store_new_key.")?; |
Janis Danisevskis | 4507f3b | 2021-01-13 16:34:39 -0800 | [diff] [blame] | 142 | if need_gc { |
| 143 | Gc::notify_gc(); |
| 144 | } |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 145 | Ok(KeyDescriptor { |
| 146 | domain: Domain::KEY_ID, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 147 | nspace: key_id.id(), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 148 | ..Default::default() |
| 149 | }) |
| 150 | }) |
| 151 | .context("In store_new_key.")?, |
| 152 | }; |
| 153 | |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 154 | Ok(KeyMetadata { |
| 155 | key, |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 156 | keySecurityLevel: self.security_level, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 157 | certificate: cert, |
| 158 | certificateChain: cert_chain, |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 159 | authorizations: crate::utils::key_parameters_to_authorizations(key_parameters), |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 160 | modificationTimeMs: creation_date.to_millis_epoch(), |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 161 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 164 | fn create_operation( |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 165 | &self, |
| 166 | key: &KeyDescriptor, |
| 167 | operation_parameters: &[KeyParameter], |
| 168 | forced: bool, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 169 | ) -> Result<CreateOperationResponse> { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 170 | let caller_uid = ThreadState::get_calling_uid(); |
| 171 | // We use `scoping_blob` to extend the life cycle of the blob loaded from the database, |
| 172 | // so that we can use it by reference like the blob provided by the key descriptor. |
| 173 | // Otherwise, we would have to clone the blob from the key descriptor. |
| 174 | let scoping_blob: Vec<u8>; |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame^] | 175 | let (km_blob, key_properties, key_id_guard) = match key.domain { |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 176 | Domain::BLOB => { |
| 177 | check_key_permission(KeyPerm::use_(), key, &None) |
| 178 | .context("In create_operation: checking use permission for Domain::BLOB.")?; |
| 179 | ( |
| 180 | match &key.blob { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 181 | Some(blob) => blob, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 182 | None => { |
| 183 | return Err(Error::sys()).context(concat!( |
| 184 | "In create_operation: Key blob must be specified when", |
| 185 | " using Domain::BLOB." |
| 186 | )) |
| 187 | } |
| 188 | }, |
| 189 | None, |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 190 | None, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 191 | ) |
| 192 | } |
| 193 | _ => { |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 194 | let (key_id_guard, mut key_entry) = DB |
| 195 | .with::<_, Result<(KeyIdGuard, KeyEntry)>>(|db| { |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 196 | db.borrow_mut().load_key_entry( |
| 197 | key.clone(), |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 198 | KeyType::Client, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 199 | KeyEntryLoadBits::KM, |
| 200 | caller_uid, |
| 201 | |k, av| check_key_permission(KeyPerm::use_(), k, &av), |
| 202 | ) |
| 203 | }) |
| 204 | .context("In create_operation: Failed to load key blob.")?; |
| 205 | scoping_blob = match key_entry.take_km_blob() { |
| 206 | Some(blob) => blob, |
| 207 | None => { |
| 208 | return Err(Error::sys()).context(concat!( |
| 209 | "In create_operation: Successfully loaded key entry,", |
| 210 | " but KM blob was missing." |
| 211 | )) |
| 212 | } |
| 213 | }; |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame^] | 214 | ( |
| 215 | &scoping_blob, |
| 216 | Some((key_id_guard.id(), key_entry.into_key_parameters())), |
| 217 | Some(key_id_guard), |
| 218 | ) |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 219 | } |
| 220 | }; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 221 | |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 222 | 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] | 223 | Err(Error::Km(ErrorCode::INVALID_ARGUMENT)) |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 224 | .context("In create_operation: No operation purpose specified."), |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame] | 225 | |kp| match kp.value { |
| 226 | KeyParameterValue::KeyPurpose(p) => Ok(p), |
| 227 | _ => Err(Error::Km(ErrorCode::INVALID_ARGUMENT)) |
| 228 | .context("In create_operation: Malformed KeyParameter."), |
| 229 | }, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 230 | )?; |
| 231 | |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 232 | let (immediate_hat, mut auth_info) = ENFORCEMENTS |
| 233 | .authorize_create( |
| 234 | purpose, |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame^] | 235 | key_properties.as_ref(), |
| 236 | operation_parameters.as_ref(), |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 237 | // TODO b/178222844 Replace this with the configuration returned by |
| 238 | // KeyMintDevice::getHardwareInfo. |
| 239 | // For now we assume that strongbox implementations need secure timestamps. |
| 240 | self.security_level == SecurityLevel::STRONGBOX, |
| 241 | ) |
| 242 | .context("In create_operation.")?; |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 243 | |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 244 | let immediate_hat = immediate_hat.unwrap_or_default(); |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 245 | |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 246 | let km_dev: Box<dyn IKeyMintDevice> = self |
| 247 | .keymint |
| 248 | .get_interface() |
| 249 | .context("In create_operation: Failed to get KeyMint device")?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 250 | |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 251 | let (begin_result, upgraded_blob) = self |
| 252 | .upgrade_keyblob_if_required_with( |
| 253 | &*km_dev, |
| 254 | key_id_guard, |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 255 | &km_blob, |
| 256 | &operation_parameters, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 257 | |blob| loop { |
| 258 | match map_km_error(km_dev.begin( |
| 259 | purpose, |
| 260 | blob, |
| 261 | &operation_parameters, |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 262 | &immediate_hat, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 263 | )) { |
| 264 | Err(Error::Km(ErrorCode::TOO_MANY_OPERATIONS)) => { |
| 265 | self.operation_db.prune(caller_uid)?; |
| 266 | continue; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 267 | } |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 268 | v => return v, |
| 269 | } |
| 270 | }, |
| 271 | ) |
| 272 | .context("In create_operation: Failed to begin operation.")?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 273 | |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 274 | let operation_challenge = auth_info.finalize_create_authorization(begin_result.challenge); |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 275 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 276 | let operation = match begin_result.operation { |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 277 | Some(km_op) => { |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 278 | self.operation_db.create_operation(km_op, caller_uid, auth_info) |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 279 | }, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 280 | None => return Err(Error::sys()).context("In create_operation: Begin operation returned successfully, but did not return a valid operation."), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | let op_binder: Box<dyn IKeystoreOperation> = |
| 284 | KeystoreOperation::new_native_binder(operation) |
| 285 | .as_binder() |
| 286 | .into_interface() |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 287 | .context("In create_operation: Failed to create IKeystoreOperation.")?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 288 | |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 289 | Ok(CreateOperationResponse { |
| 290 | iOperation: Some(op_binder), |
Hasini Gunasinghe | 888dd35 | 2020-11-17 23:08:39 +0000 | [diff] [blame] | 291 | operationChallenge: operation_challenge, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 292 | parameters: match begin_result.params.len() { |
| 293 | 0 => None, |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 294 | _ => Some(KeyParameters { keyParameter: begin_result.params }), |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 295 | }, |
| 296 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | fn generate_key( |
| 300 | &self, |
| 301 | key: &KeyDescriptor, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 302 | attestation_key: Option<&KeyDescriptor>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 303 | params: &[KeyParameter], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 304 | flags: i32, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 305 | entropy: &[u8], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 306 | ) -> Result<KeyMetadata> { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 307 | if key.domain != Domain::BLOB && key.alias.is_none() { |
| 308 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
| 309 | .context("In generate_key: Alias must be specified"); |
| 310 | } |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 311 | let caller_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 312 | |
| 313 | let key = match key.domain { |
| 314 | Domain::APP => KeyDescriptor { |
| 315 | domain: key.domain, |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 316 | nspace: caller_uid as i64, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 317 | alias: key.alias.clone(), |
| 318 | blob: None, |
| 319 | }, |
| 320 | _ => key.clone(), |
| 321 | }; |
| 322 | |
| 323 | // generate_key requires the rebind permission. |
| 324 | check_key_permission(KeyPerm::rebind(), &key, &None).context("In generate_key.")?; |
| 325 | |
| 326 | let km_dev: Box<dyn IKeyMintDevice> = self.keymint.get_interface()?; |
| 327 | map_km_error(km_dev.addRngEntropy(entropy))?; |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 328 | let creation_result = map_km_error(km_dev.generateKey(¶ms))?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 329 | |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 330 | let user_id = uid_to_android_user(caller_uid); |
| 331 | self.store_new_key(key, creation_result, user_id).context("In generate_key.") |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | fn import_key( |
| 335 | &self, |
| 336 | key: &KeyDescriptor, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 337 | attestation_key: Option<&KeyDescriptor>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 338 | params: &[KeyParameter], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 339 | flags: i32, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 340 | key_data: &[u8], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 341 | ) -> Result<KeyMetadata> { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 342 | if key.domain != Domain::BLOB && key.alias.is_none() { |
| 343 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
| 344 | .context("In import_key: Alias must be specified"); |
| 345 | } |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 346 | let caller_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 347 | |
| 348 | let key = match key.domain { |
| 349 | Domain::APP => KeyDescriptor { |
| 350 | domain: key.domain, |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 351 | nspace: caller_uid as i64, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 352 | alias: key.alias.clone(), |
| 353 | blob: None, |
| 354 | }, |
| 355 | _ => key.clone(), |
| 356 | }; |
| 357 | |
| 358 | // import_key requires the rebind permission. |
| 359 | check_key_permission(KeyPerm::rebind(), &key, &None).context("In import_key.")?; |
| 360 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 361 | let format = params |
| 362 | .iter() |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 363 | .find(|p| p.tag == Tag::ALGORITHM) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 364 | .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
| 365 | .context("No KeyParameter 'Algorithm'.") |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame] | 366 | .and_then(|p| match &p.value { |
| 367 | KeyParameterValue::Algorithm(Algorithm::AES) |
| 368 | | KeyParameterValue::Algorithm(Algorithm::HMAC) |
| 369 | | KeyParameterValue::Algorithm(Algorithm::TRIPLE_DES) => Ok(KeyFormat::RAW), |
| 370 | KeyParameterValue::Algorithm(Algorithm::RSA) |
| 371 | | KeyParameterValue::Algorithm(Algorithm::EC) => Ok(KeyFormat::PKCS8), |
| 372 | v => Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
| 373 | .context(format!("Unknown Algorithm {:?}.", v)), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 374 | }) |
| 375 | .context("In import_key.")?; |
| 376 | |
| 377 | let km_dev: Box<dyn IKeyMintDevice> = self.keymint.get_interface()?; |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 378 | let creation_result = map_km_error(km_dev.importKey(¶ms, format, key_data))?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 379 | |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 380 | let user_id = uid_to_android_user(caller_uid); |
| 381 | self.store_new_key(key, creation_result, user_id).context("In import_key.") |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | fn import_wrapped_key( |
| 385 | &self, |
| 386 | key: &KeyDescriptor, |
| 387 | wrapping_key: &KeyDescriptor, |
| 388 | masking_key: Option<&[u8]>, |
| 389 | params: &[KeyParameter], |
| 390 | authenticators: &[AuthenticatorSpec], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 391 | ) -> Result<KeyMetadata> { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 392 | if key.domain != Domain::BLOB && key.alias.is_none() { |
| 393 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
| 394 | .context("In import_wrapped_key: Alias must be specified."); |
| 395 | } |
| 396 | |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 397 | if wrapping_key.domain == Domain::BLOB { |
| 398 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)).context( |
| 399 | "In import_wrapped_key: Import wrapped key not supported for self managed blobs.", |
| 400 | ); |
| 401 | } |
| 402 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 403 | let wrapped_data = match &key.blob { |
| 404 | Some(d) => d, |
| 405 | None => { |
| 406 | return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)).context( |
| 407 | "In import_wrapped_key: Blob must be specified and hold wrapped key data.", |
| 408 | ) |
| 409 | } |
| 410 | }; |
| 411 | |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 412 | let caller_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 413 | let key = match key.domain { |
| 414 | Domain::APP => KeyDescriptor { |
| 415 | domain: key.domain, |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 416 | nspace: caller_uid as i64, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 417 | alias: key.alias.clone(), |
| 418 | blob: None, |
| 419 | }, |
| 420 | _ => key.clone(), |
| 421 | }; |
| 422 | |
| 423 | // import_wrapped_key requires the rebind permission for the new key. |
| 424 | check_key_permission(KeyPerm::rebind(), &key, &None).context("In import_wrapped_key.")?; |
| 425 | |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 426 | let (wrapping_key_id_guard, wrapping_key_entry) = DB |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 427 | .with(|db| { |
| 428 | db.borrow_mut().load_key_entry( |
| 429 | wrapping_key.clone(), |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 430 | KeyType::Client, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 431 | KeyEntryLoadBits::KM, |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 432 | caller_uid, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 433 | |k, av| check_key_permission(KeyPerm::use_(), k, &av), |
| 434 | ) |
| 435 | }) |
| 436 | .context("Failed to load wrapping key.")?; |
| 437 | let wrapping_key_blob = match wrapping_key_entry.km_blob() { |
| 438 | Some(blob) => blob, |
| 439 | None => { |
| 440 | return Err(error::Error::sys()).context(concat!( |
| 441 | "No km_blob after successfully loading key.", |
| 442 | " This should never happen." |
| 443 | )) |
| 444 | } |
| 445 | }; |
| 446 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 447 | // km_dev.importWrappedKey does not return a certificate chain. |
| 448 | // TODO Do we assume that all wrapped keys are symmetric? |
| 449 | // let certificate_chain: Vec<KmCertificate> = Default::default(); |
| 450 | |
| 451 | let pw_sid = authenticators |
| 452 | .iter() |
| 453 | .find_map(|a| match a.authenticatorType { |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 454 | HardwareAuthenticatorType::PASSWORD => Some(a.authenticatorId), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 455 | _ => None, |
| 456 | }) |
| 457 | .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
| 458 | .context("A password authenticator SID must be specified.")?; |
| 459 | |
| 460 | let fp_sid = authenticators |
| 461 | .iter() |
| 462 | .find_map(|a| match a.authenticatorType { |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 463 | HardwareAuthenticatorType::FINGERPRINT => Some(a.authenticatorId), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 464 | _ => None, |
| 465 | }) |
| 466 | .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT)) |
| 467 | .context("A fingerprint authenticator SID must be specified.")?; |
| 468 | |
| 469 | let masking_key = masking_key.unwrap_or(ZERO_BLOB_32); |
| 470 | |
| 471 | let km_dev: Box<dyn IKeyMintDevice> = self.keymint.get_interface()?; |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 472 | let (creation_result, _) = self.upgrade_keyblob_if_required_with( |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 473 | &*km_dev, |
| 474 | Some(wrapping_key_id_guard), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 475 | wrapping_key_blob, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 476 | &[], |
| 477 | |wrapping_blob| { |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 478 | let creation_result = map_km_error(km_dev.importWrappedKey( |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 479 | wrapped_data, |
| 480 | wrapping_key_blob, |
| 481 | masking_key, |
| 482 | ¶ms, |
| 483 | pw_sid, |
| 484 | fp_sid, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 485 | ))?; |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 486 | Ok(creation_result) |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 487 | }, |
| 488 | )?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 489 | |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 490 | let user_id = uid_to_android_user(caller_uid); |
| 491 | self.store_new_key(key, creation_result, user_id).context("In import_wrapped_key.") |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 492 | } |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 493 | |
| 494 | fn upgrade_keyblob_if_required_with<T, F>( |
| 495 | &self, |
| 496 | km_dev: &dyn IKeyMintDevice, |
| 497 | key_id_guard: Option<KeyIdGuard>, |
| 498 | blob: &[u8], |
| 499 | params: &[KeyParameter], |
| 500 | f: F, |
| 501 | ) -> Result<(T, Option<Vec<u8>>)> |
| 502 | where |
| 503 | F: Fn(&[u8]) -> Result<T, Error>, |
| 504 | { |
| 505 | match f(blob) { |
| 506 | Err(Error::Km(ErrorCode::KEY_REQUIRES_UPGRADE)) => { |
| 507 | let upgraded_blob = map_km_error(km_dev.upgradeKey(blob, params)) |
| 508 | .context("In upgrade_keyblob_if_required_with: Upgrade failed.")?; |
| 509 | key_id_guard.map_or(Ok(()), |key_id_guard| { |
| 510 | DB.with(|db| { |
| 511 | db.borrow_mut().insert_blob( |
| 512 | &key_id_guard, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 513 | SubComponentType::KEY_BLOB, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 514 | &upgraded_blob, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 515 | ) |
| 516 | }) |
| 517 | .context(concat!( |
| 518 | "In upgrade_keyblob_if_required_with: ", |
| 519 | "Failed to insert upgraded blob into the database.", |
| 520 | )) |
| 521 | })?; |
| 522 | match f(&upgraded_blob) { |
| 523 | Ok(v) => Ok((v, Some(upgraded_blob))), |
| 524 | Err(e) => Err(e).context(concat!( |
| 525 | "In upgrade_keyblob_if_required_with: ", |
| 526 | "Failed to perform operation on second try." |
| 527 | )), |
| 528 | } |
| 529 | } |
| 530 | Err(e) => { |
| 531 | Err(e).context("In upgrade_keyblob_if_required_with: Failed perform operation.") |
| 532 | } |
| 533 | Ok(v) => Ok((v, None)), |
| 534 | } |
| 535 | } |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | impl binder::Interface for KeystoreSecurityLevel {} |
| 539 | |
| 540 | impl IKeystoreSecurityLevel for KeystoreSecurityLevel { |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 541 | fn createOperation( |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 542 | &self, |
| 543 | key: &KeyDescriptor, |
| 544 | operation_parameters: &[KeyParameter], |
| 545 | forced: bool, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 546 | ) -> binder::public_api::Result<CreateOperationResponse> { |
| 547 | map_or_log_err(self.create_operation(key, operation_parameters, forced), Ok) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 548 | } |
| 549 | fn generateKey( |
| 550 | &self, |
| 551 | key: &KeyDescriptor, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 552 | attestation_key: Option<&KeyDescriptor>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 553 | params: &[KeyParameter], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 554 | flags: i32, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 555 | entropy: &[u8], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 556 | ) -> binder::public_api::Result<KeyMetadata> { |
| 557 | map_or_log_err(self.generate_key(key, attestation_key, params, flags, entropy), Ok) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 558 | } |
| 559 | fn importKey( |
| 560 | &self, |
| 561 | key: &KeyDescriptor, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 562 | attestation_key: Option<&KeyDescriptor>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 563 | params: &[KeyParameter], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 564 | flags: i32, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 565 | key_data: &[u8], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 566 | ) -> binder::public_api::Result<KeyMetadata> { |
| 567 | map_or_log_err(self.import_key(key, attestation_key, params, flags, key_data), Ok) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 568 | } |
| 569 | fn importWrappedKey( |
| 570 | &self, |
| 571 | key: &KeyDescriptor, |
| 572 | wrapping_key: &KeyDescriptor, |
| 573 | masking_key: Option<&[u8]>, |
| 574 | params: &[KeyParameter], |
| 575 | authenticators: &[AuthenticatorSpec], |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 576 | ) -> binder::public_api::Result<KeyMetadata> { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 577 | map_or_log_err( |
| 578 | self.import_wrapped_key(key, wrapping_key, masking_key, params, authenticators), |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 579 | Ok, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 580 | ) |
| 581 | } |
| 582 | } |