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