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