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