Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 1 | // Copyright 2020, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 15 | //! This crate implement the core Keystore 2.0 service API as defined by the Keystore 2.0 |
| 16 | //! AIDL spec. |
| 17 | |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 18 | use std::collections::HashMap; |
| 19 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 20 | use crate::error::{self, map_or_log_err, ErrorCode}; |
Janis Danisevskis | e92a5e6 | 2020-12-02 12:57:41 -0800 | [diff] [blame] | 21 | use crate::permission::{KeyPerm, KeystorePerm}; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 22 | use crate::security_level::KeystoreSecurityLevel; |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 23 | use crate::utils::{ |
Janis Danisevskis | e92a5e6 | 2020-12-02 12:57:41 -0800 | [diff] [blame] | 24 | check_grant_permission, check_key_permission, check_keystore_permission, |
| 25 | key_parameters_to_authorizations, Asp, |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 26 | }; |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 27 | use crate::{ |
| 28 | database::Uuid, |
| 29 | globals::{create_thread_local_db, DB, LEGACY_BLOB_LOADER, LEGACY_MIGRATOR}, |
| 30 | }; |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 31 | use crate::{database::KEYSTORE_UUID, permission}; |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 32 | use crate::{ |
| 33 | database::{KeyEntryLoadBits, KeyType, SubComponentType}, |
| 34 | error::ResponseCode, |
| 35 | }; |
Shawn Willden | 708744a | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 36 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::SecurityLevel::SecurityLevel; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 37 | use android_system_keystore2::aidl::android::system::keystore2::{ |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 38 | Domain::Domain, IKeystoreSecurityLevel::IKeystoreSecurityLevel, |
| 39 | IKeystoreService::BnKeystoreService, IKeystoreService::IKeystoreService, |
| 40 | KeyDescriptor::KeyDescriptor, KeyEntryResponse::KeyEntryResponse, KeyMetadata::KeyMetadata, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 41 | }; |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 42 | use anyhow::{Context, Result}; |
Andrew Walbran | 808e860 | 2021-03-16 13:58:28 +0000 | [diff] [blame] | 43 | use binder::{IBinderInternal, Strong, ThreadState}; |
Janis Danisevskis | e92a5e6 | 2020-12-02 12:57:41 -0800 | [diff] [blame] | 44 | use error::Error; |
| 45 | use keystore2_selinux as selinux; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 46 | |
| 47 | /// Implementation of the IKeystoreService. |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 48 | #[derive(Default)] |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 49 | pub struct KeystoreService { |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 50 | i_sec_level_by_uuid: HashMap<Uuid, Asp>, |
| 51 | uuid_by_sec_level: HashMap<SecurityLevel, Uuid>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | impl KeystoreService { |
| 55 | /// Create a new instance of the Keystore 2.0 service. |
Stephen Crane | 221bbb5 | 2020-12-16 15:52:10 -0800 | [diff] [blame] | 56 | pub fn new_native_binder() -> Result<Strong<dyn IKeystoreService>> { |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 57 | let mut result: Self = Default::default(); |
| 58 | let (dev, uuid) = |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 59 | KeystoreSecurityLevel::new_native_binder(SecurityLevel::TRUSTED_ENVIRONMENT) |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 60 | .context(concat!( |
| 61 | "In KeystoreService::new_native_binder: ", |
| 62 | "Trying to construct mandatory security level TEE." |
| 63 | )) |
| 64 | .map(|(dev, uuid)| (Asp::new(dev.as_binder()), uuid))?; |
| 65 | result.i_sec_level_by_uuid.insert(uuid, dev); |
| 66 | result.uuid_by_sec_level.insert(SecurityLevel::TRUSTED_ENVIRONMENT, uuid); |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 67 | |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 68 | // Strongbox is optional, so we ignore errors and turn the result into an Option. |
| 69 | if let Ok((dev, uuid)) = KeystoreSecurityLevel::new_native_binder(SecurityLevel::STRONGBOX) |
| 70 | .map(|(dev, uuid)| (Asp::new(dev.as_binder()), uuid)) |
| 71 | { |
| 72 | result.i_sec_level_by_uuid.insert(uuid, dev); |
| 73 | result.uuid_by_sec_level.insert(SecurityLevel::STRONGBOX, uuid); |
| 74 | } |
| 75 | |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 76 | let uuid_by_sec_level = result.uuid_by_sec_level.clone(); |
| 77 | LEGACY_MIGRATOR |
| 78 | .set_init(move || { |
| 79 | (create_thread_local_db(), uuid_by_sec_level, LEGACY_BLOB_LOADER.clone()) |
| 80 | }) |
| 81 | .context( |
| 82 | "In KeystoreService::new_native_binder: Trying to initialize the legacy migrator.", |
| 83 | )?; |
| 84 | |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 85 | let result = BnKeystoreService::new_binder(result); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 86 | result.as_binder().set_requesting_sid(true); |
| 87 | Ok(result) |
| 88 | } |
| 89 | |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 90 | fn uuid_to_sec_level(&self, uuid: &Uuid) -> SecurityLevel { |
| 91 | self.uuid_by_sec_level |
| 92 | .iter() |
| 93 | .find(|(_, v)| **v == *uuid) |
| 94 | .map(|(s, _)| *s) |
| 95 | .unwrap_or(SecurityLevel::SOFTWARE) |
| 96 | } |
| 97 | |
Stephen Crane | 221bbb5 | 2020-12-16 15:52:10 -0800 | [diff] [blame] | 98 | fn get_i_sec_level_by_uuid(&self, uuid: &Uuid) -> Result<Strong<dyn IKeystoreSecurityLevel>> { |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 99 | if let Some(dev) = self.i_sec_level_by_uuid.get(uuid) { |
| 100 | dev.get_interface().context("In get_i_sec_level_by_uuid.") |
| 101 | } else { |
| 102 | Err(error::Error::sys()) |
| 103 | .context("In get_i_sec_level_by_uuid: KeyMint instance for key not found.") |
| 104 | } |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 107 | fn get_security_level( |
| 108 | &self, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 109 | sec_level: SecurityLevel, |
Stephen Crane | 221bbb5 | 2020-12-16 15:52:10 -0800 | [diff] [blame] | 110 | ) -> Result<Strong<dyn IKeystoreSecurityLevel>> { |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 111 | if let Some(dev) = self |
| 112 | .uuid_by_sec_level |
| 113 | .get(&sec_level) |
| 114 | .and_then(|uuid| self.i_sec_level_by_uuid.get(uuid)) |
| 115 | { |
| 116 | dev.get_interface().context("In get_security_level.") |
| 117 | } else { |
| 118 | Err(error::Error::Km(ErrorCode::HARDWARE_TYPE_UNAVAILABLE)) |
| 119 | .context("In get_security_level: No such security level.") |
| 120 | } |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 123 | fn get_key_entry(&self, key: &KeyDescriptor) -> Result<KeyEntryResponse> { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 124 | let caller_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 125 | let (key_id_guard, mut key_entry) = DB |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 126 | .with(|db| { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 127 | LEGACY_MIGRATOR.with_try_migrate(&key, caller_uid, || { |
| 128 | db.borrow_mut().load_key_entry( |
| 129 | &key, |
| 130 | KeyType::Client, |
| 131 | KeyEntryLoadBits::PUBLIC, |
| 132 | caller_uid, |
| 133 | |k, av| check_key_permission(KeyPerm::get_info(), k, &av), |
| 134 | ) |
| 135 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 136 | }) |
| 137 | .context("In get_key_entry, while trying to load key info.")?; |
| 138 | |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 139 | let i_sec_level = if !key_entry.pure_cert() { |
| 140 | Some( |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 141 | self.get_i_sec_level_by_uuid(key_entry.km_uuid()) |
| 142 | .context("In get_key_entry: Trying to get security level proxy.")?, |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 143 | ) |
| 144 | } else { |
| 145 | None |
| 146 | }; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 147 | |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 148 | Ok(KeyEntryResponse { |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 149 | iSecurityLevel: i_sec_level, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 150 | metadata: KeyMetadata { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 151 | key: KeyDescriptor { |
| 152 | domain: Domain::KEY_ID, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 153 | nspace: key_id_guard.id(), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 154 | ..Default::default() |
| 155 | }, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 156 | keySecurityLevel: self.uuid_to_sec_level(key_entry.km_uuid()), |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 157 | certificate: key_entry.take_cert(), |
| 158 | certificateChain: key_entry.take_cert_chain(), |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 159 | modificationTimeMs: key_entry |
| 160 | .metadata() |
| 161 | .creation_date() |
| 162 | .map(|d| d.to_millis_epoch()) |
| 163 | .ok_or(Error::Rc(ResponseCode::VALUE_CORRUPTED)) |
| 164 | .context("In get_key_entry: Trying to get creation date.")?, |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 165 | authorizations: key_parameters_to_authorizations(key_entry.into_key_parameters()), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 166 | }, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 167 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | fn update_subcomponent( |
| 171 | &self, |
| 172 | key: &KeyDescriptor, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 173 | public_cert: Option<&[u8]>, |
| 174 | certificate_chain: Option<&[u8]>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 175 | ) -> Result<()> { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 176 | let caller_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 177 | DB.with::<_, Result<()>>(|db| { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 178 | let entry = match LEGACY_MIGRATOR.with_try_migrate(&key, caller_uid, || { |
| 179 | db.borrow_mut().load_key_entry( |
| 180 | &key, |
| 181 | KeyType::Client, |
| 182 | KeyEntryLoadBits::NONE, |
| 183 | caller_uid, |
| 184 | |k, av| { |
| 185 | check_key_permission(KeyPerm::update(), k, &av) |
| 186 | .context("In update_subcomponent.") |
| 187 | }, |
| 188 | ) |
| 189 | }) { |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 190 | Err(e) => match e.root_cause().downcast_ref::<Error>() { |
| 191 | Some(Error::Rc(ResponseCode::KEY_NOT_FOUND)) => Ok(None), |
| 192 | _ => Err(e), |
| 193 | }, |
| 194 | Ok(v) => Ok(Some(v)), |
| 195 | } |
| 196 | .context("Failed to load key entry.")?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 197 | |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 198 | let mut db = db.borrow_mut(); |
Paul Crowley | d5653e5 | 2021-03-25 09:46:31 -0700 | [diff] [blame^] | 199 | if let Some((key_id_guard, _key_entry)) = entry { |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 200 | db.set_blob(&key_id_guard, SubComponentType::CERT, public_cert, None) |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 201 | .context("Failed to update cert subcomponent.")?; |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 202 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 203 | db.set_blob(&key_id_guard, SubComponentType::CERT_CHAIN, certificate_chain, None) |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 204 | .context("Failed to update cert chain subcomponent.")?; |
| 205 | return Ok(()); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 208 | // If we reach this point we have to check the special condition where a certificate |
| 209 | // entry may be made. |
| 210 | if !(public_cert.is_none() && certificate_chain.is_some()) { |
| 211 | return Err(Error::Rc(ResponseCode::KEY_NOT_FOUND)).context("No key to update."); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 212 | } |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 213 | |
| 214 | // So we know that we have a certificate chain and no public cert. |
| 215 | // Now check that we have everything we need to make a new certificate entry. |
| 216 | let key = match (key.domain, &key.alias) { |
| 217 | (Domain::APP, Some(ref alias)) => KeyDescriptor { |
| 218 | domain: Domain::APP, |
| 219 | nspace: ThreadState::get_calling_uid() as i64, |
| 220 | alias: Some(alias.clone()), |
| 221 | blob: None, |
| 222 | }, |
| 223 | (Domain::SELINUX, Some(_)) => key.clone(), |
| 224 | _ => { |
| 225 | return Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)) |
| 226 | .context("Domain must be APP or SELINUX to insert a certificate.") |
| 227 | } |
| 228 | }; |
| 229 | |
| 230 | // Security critical: This must return on failure. Do not remove the `?`; |
| 231 | check_key_permission(KeyPerm::rebind(), &key, &None) |
| 232 | .context("Caller does not have permission to insert this certificate.")?; |
| 233 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 234 | db.store_new_certificate(&key, certificate_chain.unwrap(), &KEYSTORE_UUID) |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 235 | .context("Failed to insert new certificate.")?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 236 | Ok(()) |
| 237 | }) |
| 238 | .context("In update_subcomponent.") |
| 239 | } |
| 240 | |
| 241 | fn list_entries(&self, domain: Domain, namespace: i64) -> Result<Vec<KeyDescriptor>> { |
Janis Danisevskis | e92a5e6 | 2020-12-02 12:57:41 -0800 | [diff] [blame] | 242 | let mut k = match domain { |
| 243 | Domain::APP => KeyDescriptor { |
| 244 | domain, |
| 245 | nspace: ThreadState::get_calling_uid() as u64 as i64, |
| 246 | ..Default::default() |
| 247 | }, |
| 248 | Domain::SELINUX => KeyDescriptor{domain, nspace: namespace, ..Default::default()}, |
| 249 | _ => return Err(Error::perm()).context( |
| 250 | "In list_entries: List entries is only supported for Domain::APP and Domain::SELINUX." |
| 251 | ), |
| 252 | }; |
| 253 | |
| 254 | // First we check if the caller has the info permission for the selected domain/namespace. |
| 255 | // By default we use the calling uid as namespace if domain is Domain::APP. |
| 256 | // If the first check fails we check if the caller has the list permission allowing to list |
| 257 | // any namespace. In that case we also adjust the queried namespace if a specific uid was |
| 258 | // selected. |
| 259 | match check_key_permission(KeyPerm::get_info(), &k, &None) { |
| 260 | Err(e) => { |
| 261 | if let Some(selinux::Error::PermissionDenied) = |
| 262 | e.root_cause().downcast_ref::<selinux::Error>() |
| 263 | { |
| 264 | check_keystore_permission(KeystorePerm::list()) |
| 265 | .context("In list_entries: While checking keystore permission.")?; |
| 266 | if namespace != -1 { |
| 267 | k.nspace = namespace; |
| 268 | } |
| 269 | } else { |
| 270 | return Err(e).context("In list_entries: While checking key permission.")?; |
| 271 | } |
| 272 | } |
| 273 | Ok(()) => {} |
| 274 | }; |
| 275 | |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 276 | let mut result = LEGACY_MIGRATOR |
| 277 | .list_uid(k.domain, k.nspace) |
| 278 | .context("In list_entries: Trying to list legacy keys.")?; |
| 279 | |
| 280 | result.append( |
| 281 | &mut DB |
| 282 | .with(|db| { |
| 283 | let mut db = db.borrow_mut(); |
| 284 | db.list(k.domain, k.nspace) |
| 285 | }) |
| 286 | .context("In list_entries: Trying to list keystore database.")?, |
| 287 | ); |
| 288 | |
| 289 | result.sort_unstable(); |
| 290 | result.dedup(); |
| 291 | Ok(result) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | fn delete_key(&self, key: &KeyDescriptor) -> Result<()> { |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 295 | let caller_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 296 | DB.with(|db| { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 297 | LEGACY_MIGRATOR.with_try_migrate(&key, caller_uid, || { |
| 298 | db.borrow_mut().unbind_key(&key, KeyType::Client, caller_uid, |k, av| { |
| 299 | check_key_permission(KeyPerm::delete(), k, &av).context("During delete_key.") |
| 300 | }) |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 301 | }) |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 302 | }) |
| 303 | .context("In delete_key: Trying to unbind the key.")?; |
Janis Danisevskis | a51ccbc | 2020-11-25 21:04:24 -0800 | [diff] [blame] | 304 | Ok(()) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | fn grant( |
| 308 | &self, |
| 309 | key: &KeyDescriptor, |
| 310 | grantee_uid: i32, |
| 311 | access_vector: permission::KeyPermSet, |
| 312 | ) -> Result<KeyDescriptor> { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 313 | let caller_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 314 | DB.with(|db| { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 315 | LEGACY_MIGRATOR.with_try_migrate(&key, caller_uid, || { |
| 316 | db.borrow_mut().grant( |
| 317 | &key, |
| 318 | caller_uid, |
| 319 | grantee_uid as u32, |
| 320 | access_vector, |
| 321 | |k, av| check_grant_permission(*av, k).context("During grant."), |
| 322 | ) |
| 323 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 324 | }) |
| 325 | .context("In KeystoreService::grant.") |
| 326 | } |
| 327 | |
| 328 | fn ungrant(&self, key: &KeyDescriptor, grantee_uid: i32) -> Result<()> { |
| 329 | DB.with(|db| { |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 330 | db.borrow_mut().ungrant(&key, ThreadState::get_calling_uid(), grantee_uid as u32, |k| { |
| 331 | check_key_permission(KeyPerm::grant(), k, &None) |
| 332 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 333 | }) |
| 334 | .context("In KeystoreService::ungrant.") |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | impl binder::Interface for KeystoreService {} |
| 339 | |
| 340 | // Implementation of IKeystoreService. See AIDL spec at |
| 341 | // system/security/keystore2/binder/android/security/keystore2/IKeystoreService.aidl |
| 342 | impl IKeystoreService for KeystoreService { |
| 343 | fn getSecurityLevel( |
| 344 | &self, |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 345 | security_level: SecurityLevel, |
Stephen Crane | 221bbb5 | 2020-12-16 15:52:10 -0800 | [diff] [blame] | 346 | ) -> binder::public_api::Result<Strong<dyn IKeystoreSecurityLevel>> { |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 347 | map_or_log_err(self.get_security_level(security_level), Ok) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 348 | } |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 349 | fn getKeyEntry(&self, key: &KeyDescriptor) -> binder::public_api::Result<KeyEntryResponse> { |
| 350 | map_or_log_err(self.get_key_entry(key), Ok) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 351 | } |
| 352 | fn updateSubcomponent( |
| 353 | &self, |
| 354 | key: &KeyDescriptor, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 355 | public_cert: Option<&[u8]>, |
| 356 | certificate_chain: Option<&[u8]>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 357 | ) -> binder::public_api::Result<()> { |
| 358 | map_or_log_err(self.update_subcomponent(key, public_cert, certificate_chain), Ok) |
| 359 | } |
| 360 | fn listEntries( |
| 361 | &self, |
| 362 | domain: Domain, |
| 363 | namespace: i64, |
| 364 | ) -> binder::public_api::Result<Vec<KeyDescriptor>> { |
| 365 | map_or_log_err(self.list_entries(domain, namespace), Ok) |
| 366 | } |
| 367 | fn deleteKey(&self, key: &KeyDescriptor) -> binder::public_api::Result<()> { |
| 368 | map_or_log_err(self.delete_key(key), Ok) |
| 369 | } |
| 370 | fn grant( |
| 371 | &self, |
| 372 | key: &KeyDescriptor, |
| 373 | grantee_uid: i32, |
| 374 | access_vector: i32, |
| 375 | ) -> binder::public_api::Result<KeyDescriptor> { |
| 376 | map_or_log_err(self.grant(key, grantee_uid, access_vector.into()), Ok) |
| 377 | } |
| 378 | fn ungrant(&self, key: &KeyDescriptor, grantee_uid: i32) -> binder::public_api::Result<()> { |
| 379 | map_or_log_err(self.ungrant(key, grantee_uid), Ok) |
| 380 | } |
| 381 | } |