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, |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 25 | key_parameters_to_authorizations, list_key_entries, uid_to_android_user, 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, |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 29 | globals::{create_thread_local_db, DB, LEGACY_BLOB_LOADER, LEGACY_IMPORTER, SUPER_KEY}, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 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(); |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 84 | LEGACY_IMPORTER |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 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 | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 133 | |
| 134 | let super_key = |
| 135 | SUPER_KEY.read().unwrap().get_per_boot_key_by_user_id(uid_to_android_user(caller_uid)); |
| 136 | |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 137 | let (key_id_guard, mut key_entry) = DB |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 138 | .with(|db| { |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 139 | LEGACY_IMPORTER.with_try_import(key, caller_uid, super_key, || { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 140 | db.borrow_mut().load_key_entry( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 141 | key, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 142 | KeyType::Client, |
| 143 | KeyEntryLoadBits::PUBLIC, |
| 144 | caller_uid, |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 145 | |k, av| check_key_permission(KeyPerm::GetInfo, k, &av), |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 146 | ) |
| 147 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 148 | }) |
| 149 | .context("In get_key_entry, while trying to load key info.")?; |
| 150 | |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 151 | let i_sec_level = if !key_entry.pure_cert() { |
| 152 | Some( |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 153 | self.get_i_sec_level_by_uuid(key_entry.km_uuid()) |
| 154 | .context("In get_key_entry: Trying to get security level proxy.")?, |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 155 | ) |
| 156 | } else { |
| 157 | None |
| 158 | }; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 159 | |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 160 | Ok(KeyEntryResponse { |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 161 | iSecurityLevel: i_sec_level, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 162 | metadata: KeyMetadata { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 163 | key: KeyDescriptor { |
| 164 | domain: Domain::KEY_ID, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 165 | nspace: key_id_guard.id(), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 166 | ..Default::default() |
| 167 | }, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 168 | keySecurityLevel: self.uuid_to_sec_level(key_entry.km_uuid()), |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 169 | certificate: key_entry.take_cert(), |
| 170 | certificateChain: key_entry.take_cert_chain(), |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 171 | modificationTimeMs: key_entry |
| 172 | .metadata() |
| 173 | .creation_date() |
| 174 | .map(|d| d.to_millis_epoch()) |
| 175 | .ok_or(Error::Rc(ResponseCode::VALUE_CORRUPTED)) |
| 176 | .context("In get_key_entry: Trying to get creation date.")?, |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 177 | authorizations: key_parameters_to_authorizations(key_entry.into_key_parameters()), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 178 | }, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 179 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | fn update_subcomponent( |
| 183 | &self, |
| 184 | key: &KeyDescriptor, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 185 | public_cert: Option<&[u8]>, |
| 186 | certificate_chain: Option<&[u8]>, |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 187 | ) -> Result<()> { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 188 | let caller_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 189 | let super_key = |
| 190 | SUPER_KEY.read().unwrap().get_per_boot_key_by_user_id(uid_to_android_user(caller_uid)); |
| 191 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 192 | DB.with::<_, Result<()>>(|db| { |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 193 | let entry = match LEGACY_IMPORTER.with_try_import(key, caller_uid, super_key, || { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 194 | db.borrow_mut().load_key_entry( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 195 | key, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 196 | KeyType::Client, |
| 197 | KeyEntryLoadBits::NONE, |
| 198 | caller_uid, |
| 199 | |k, av| { |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 200 | check_key_permission(KeyPerm::Update, k, &av) |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 201 | .context("In update_subcomponent.") |
| 202 | }, |
| 203 | ) |
| 204 | }) { |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 205 | Err(e) => match e.root_cause().downcast_ref::<Error>() { |
| 206 | Some(Error::Rc(ResponseCode::KEY_NOT_FOUND)) => Ok(None), |
| 207 | _ => Err(e), |
| 208 | }, |
| 209 | Ok(v) => Ok(Some(v)), |
| 210 | } |
| 211 | .context("Failed to load key entry.")?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 212 | |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 213 | let mut db = db.borrow_mut(); |
Paul Crowley | d5653e5 | 2021-03-25 09:46:31 -0700 | [diff] [blame] | 214 | if let Some((key_id_guard, _key_entry)) = entry { |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 215 | db.set_blob(&key_id_guard, SubComponentType::CERT, public_cert, None) |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 216 | .context("Failed to update cert subcomponent.")?; |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 217 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 218 | db.set_blob(&key_id_guard, SubComponentType::CERT_CHAIN, certificate_chain, None) |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 219 | .context("Failed to update cert chain subcomponent.")?; |
| 220 | return Ok(()); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 223 | // If we reach this point we have to check the special condition where a certificate |
| 224 | // entry may be made. |
| 225 | if !(public_cert.is_none() && certificate_chain.is_some()) { |
| 226 | 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] | 227 | } |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 228 | |
| 229 | // So we know that we have a certificate chain and no public cert. |
| 230 | // Now check that we have everything we need to make a new certificate entry. |
| 231 | let key = match (key.domain, &key.alias) { |
| 232 | (Domain::APP, Some(ref alias)) => KeyDescriptor { |
| 233 | domain: Domain::APP, |
| 234 | nspace: ThreadState::get_calling_uid() as i64, |
| 235 | alias: Some(alias.clone()), |
| 236 | blob: None, |
| 237 | }, |
| 238 | (Domain::SELINUX, Some(_)) => key.clone(), |
| 239 | _ => { |
| 240 | return Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)) |
| 241 | .context("Domain must be APP or SELINUX to insert a certificate.") |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | // Security critical: This must return on failure. Do not remove the `?`; |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 246 | check_key_permission(KeyPerm::Rebind, &key, &None) |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 247 | .context("Caller does not have permission to insert this certificate.")?; |
| 248 | |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 249 | db.store_new_certificate( |
| 250 | &key, |
| 251 | KeyType::Client, |
| 252 | certificate_chain.unwrap(), |
| 253 | &KEYSTORE_UUID, |
| 254 | ) |
| 255 | .context("Failed to insert new certificate.")?; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 256 | Ok(()) |
| 257 | }) |
| 258 | .context("In update_subcomponent.") |
| 259 | } |
| 260 | |
| 261 | fn list_entries(&self, domain: Domain, namespace: i64) -> Result<Vec<KeyDescriptor>> { |
Janis Danisevskis | e92a5e6 | 2020-12-02 12:57:41 -0800 | [diff] [blame] | 262 | let mut k = match domain { |
| 263 | Domain::APP => KeyDescriptor { |
| 264 | domain, |
| 265 | nspace: ThreadState::get_calling_uid() as u64 as i64, |
| 266 | ..Default::default() |
| 267 | }, |
| 268 | Domain::SELINUX => KeyDescriptor{domain, nspace: namespace, ..Default::default()}, |
| 269 | _ => return Err(Error::perm()).context( |
| 270 | "In list_entries: List entries is only supported for Domain::APP and Domain::SELINUX." |
| 271 | ), |
| 272 | }; |
| 273 | |
| 274 | // First we check if the caller has the info permission for the selected domain/namespace. |
| 275 | // By default we use the calling uid as namespace if domain is Domain::APP. |
| 276 | // If the first check fails we check if the caller has the list permission allowing to list |
| 277 | // any namespace. In that case we also adjust the queried namespace if a specific uid was |
| 278 | // selected. |
Chris Wailes | 20f50df | 2022-04-19 17:23:52 -0700 | [diff] [blame^] | 279 | if let Err(e) = check_key_permission(KeyPerm::GetInfo, &k, &None) { |
| 280 | if let Some(selinux::Error::PermissionDenied) = |
| 281 | e.root_cause().downcast_ref::<selinux::Error>() { |
| 282 | |
| 283 | check_keystore_permission(KeystorePerm::List) |
| 284 | .context("In list_entries: While checking keystore permission.")?; |
| 285 | if namespace != -1 { |
| 286 | k.nspace = namespace; |
Janis Danisevskis | e92a5e6 | 2020-12-02 12:57:41 -0800 | [diff] [blame] | 287 | } |
Chris Wailes | 20f50df | 2022-04-19 17:23:52 -0700 | [diff] [blame^] | 288 | } else { |
| 289 | return Err(e).context("In list_entries: While checking key permission.")?; |
Janis Danisevskis | e92a5e6 | 2020-12-02 12:57:41 -0800 | [diff] [blame] | 290 | } |
Chris Wailes | 20f50df | 2022-04-19 17:23:52 -0700 | [diff] [blame^] | 291 | } |
Janis Danisevskis | e92a5e6 | 2020-12-02 12:57:41 -0800 | [diff] [blame] | 292 | |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 293 | 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] | 294 | } |
| 295 | |
| 296 | fn delete_key(&self, key: &KeyDescriptor) -> Result<()> { |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 297 | let caller_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 298 | let super_key = |
| 299 | SUPER_KEY.read().unwrap().get_per_boot_key_by_user_id(uid_to_android_user(caller_uid)); |
| 300 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 301 | DB.with(|db| { |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 302 | LEGACY_IMPORTER.with_try_import(key, caller_uid, super_key, || { |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 303 | db.borrow_mut().unbind_key(key, KeyType::Client, caller_uid, |k, av| { |
Janis Danisevskis | 39d57e7 | 2021-10-19 16:56:20 -0700 | [diff] [blame] | 304 | check_key_permission(KeyPerm::Delete, k, &av).context("During delete_key.") |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 305 | }) |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 306 | }) |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 307 | }) |
| 308 | .context("In delete_key: Trying to unbind the key.")?; |
Janis Danisevskis | a51ccbc | 2020-11-25 21:04:24 -0800 | [diff] [blame] | 309 | Ok(()) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | fn grant( |
| 313 | &self, |
| 314 | key: &KeyDescriptor, |
| 315 | grantee_uid: i32, |
| 316 | access_vector: permission::KeyPermSet, |
| 317 | ) -> Result<KeyDescriptor> { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 318 | let caller_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 319 | let super_key = |
| 320 | SUPER_KEY.read().unwrap().get_per_boot_key_by_user_id(uid_to_android_user(caller_uid)); |
| 321 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 322 | DB.with(|db| { |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 323 | LEGACY_IMPORTER.with_try_import(key, caller_uid, super_key, || { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 324 | db.borrow_mut().grant( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 325 | key, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 326 | caller_uid, |
| 327 | grantee_uid as u32, |
| 328 | access_vector, |
| 329 | |k, av| check_grant_permission(*av, k).context("During grant."), |
| 330 | ) |
| 331 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 332 | }) |
| 333 | .context("In KeystoreService::grant.") |
| 334 | } |
| 335 | |
| 336 | fn ungrant(&self, key: &KeyDescriptor, grantee_uid: i32) -> Result<()> { |
| 337 | DB.with(|db| { |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 338 | 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] | 339 | check_key_permission(KeyPerm::Grant, k, &None) |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 340 | }) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 341 | }) |
| 342 | .context("In KeystoreService::ungrant.") |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | impl binder::Interface for KeystoreService {} |
| 347 | |
| 348 | // Implementation of IKeystoreService. See AIDL spec at |
| 349 | // system/security/keystore2/binder/android/security/keystore2/IKeystoreService.aidl |
| 350 | impl IKeystoreService for KeystoreService { |
| 351 | fn getSecurityLevel( |
| 352 | &self, |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 353 | security_level: SecurityLevel, |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 354 | ) -> binder::Result<Strong<dyn IKeystoreSecurityLevel>> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 355 | let _wp = wd::watch_millis_with("IKeystoreService::getSecurityLevel", 500, move || { |
| 356 | format!("security_level: {}", security_level.0) |
| 357 | }); |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 358 | map_or_log_err(self.get_security_level(security_level), Ok) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 359 | } |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 360 | fn getKeyEntry(&self, key: &KeyDescriptor) -> binder::Result<KeyEntryResponse> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 361 | let _wp = wd::watch_millis("IKeystoreService::get_key_entry", 500); |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 362 | map_or_log_err(self.get_key_entry(key), Ok) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 363 | } |
| 364 | fn updateSubcomponent( |
| 365 | &self, |
| 366 | key: &KeyDescriptor, |
Janis Danisevskis | 2c7f962 | 2020-09-30 16:30:31 -0700 | [diff] [blame] | 367 | public_cert: Option<&[u8]>, |
| 368 | certificate_chain: Option<&[u8]>, |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 369 | ) -> binder::Result<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 370 | let _wp = wd::watch_millis("IKeystoreService::updateSubcomponent", 500); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 371 | map_or_log_err(self.update_subcomponent(key, public_cert, certificate_chain), Ok) |
| 372 | } |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 373 | fn listEntries(&self, domain: Domain, namespace: i64) -> binder::Result<Vec<KeyDescriptor>> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 374 | let _wp = wd::watch_millis("IKeystoreService::listEntries", 500); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 375 | map_or_log_err(self.list_entries(domain, namespace), Ok) |
| 376 | } |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 377 | fn deleteKey(&self, key: &KeyDescriptor) -> binder::Result<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 378 | let _wp = wd::watch_millis("IKeystoreService::deleteKey", 500); |
Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 379 | let result = self.delete_key(key); |
| 380 | log_key_deleted(key, ThreadState::get_calling_uid(), result.is_ok()); |
| 381 | map_or_log_err(result, Ok) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 382 | } |
| 383 | fn grant( |
| 384 | &self, |
| 385 | key: &KeyDescriptor, |
| 386 | grantee_uid: i32, |
| 387 | access_vector: i32, |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 388 | ) -> binder::Result<KeyDescriptor> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 389 | let _wp = wd::watch_millis("IKeystoreService::grant", 500); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 390 | map_or_log_err(self.grant(key, grantee_uid, access_vector.into()), Ok) |
| 391 | } |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 392 | fn ungrant(&self, key: &KeyDescriptor, grantee_uid: i32) -> binder::Result<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 393 | let _wp = wd::watch_millis("IKeystoreService::ungrant", 500); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 394 | map_or_log_err(self.ungrant(key, grantee_uid), Ok) |
| 395 | } |
| 396 | } |