Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -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 | //! This module holds global state of Keystore such as the thread local |
| 16 | //! database connections and connections to services that Keystore needs |
| 17 | //! to talk to. |
| 18 | |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 19 | use crate::gc::Gc; |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 20 | use crate::legacy_blob::LegacyBlobLoader; |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 21 | use crate::legacy_migrator::LegacyMigrator; |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 22 | use crate::super_key::SuperKeyManager; |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 23 | use crate::utils::Asp; |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 24 | use crate::{async_task::AsyncTask, database::MonotonicRawTime}; |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 25 | use crate::{ |
| 26 | database::KeystoreDB, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 27 | database::Uuid, |
Janis Danisevskis | 8c6378e | 2021-01-01 09:30:37 -0800 | [diff] [blame] | 28 | error::{map_binder_status, map_binder_status_code, Error, ErrorCode}, |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 29 | }; |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 30 | use crate::{enforcements::Enforcements, error::map_km_error}; |
| 31 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame] | 32 | IKeyMintDevice::IKeyMintDevice, IRemotelyProvisionedComponent::IRemotelyProvisionedComponent, |
| 33 | KeyMintHardwareInfo::KeyMintHardwareInfo, SecurityLevel::SecurityLevel, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 34 | }; |
Stephen Crane | 221bbb5 | 2020-12-16 15:52:10 -0800 | [diff] [blame] | 35 | use android_hardware_security_keymint::binder::{StatusCode, Strong}; |
Janis Danisevskis | 8c6378e | 2021-01-01 09:30:37 -0800 | [diff] [blame] | 36 | use android_security_compat::aidl::android::security::compat::IKeystoreCompatService::IKeystoreCompatService; |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 37 | use anyhow::{Context, Result}; |
Janis Danisevskis | ef14e1a | 2021-02-23 23:16:55 -0800 | [diff] [blame] | 38 | use keystore2_vintf::get_aidl_instances; |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 39 | use lazy_static::lazy_static; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 40 | use std::sync::{Arc, Mutex}; |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 41 | use std::{cell::RefCell, sync::Once}; |
Janis Danisevskis | 3f2955c | 2021-02-02 21:53:35 -0800 | [diff] [blame] | 42 | use std::{collections::HashMap, path::Path, path::PathBuf}; |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 43 | |
| 44 | static DB_INIT: Once = Once::new(); |
| 45 | |
| 46 | /// Open a connection to the Keystore 2.0 database. This is called during the initialization of |
| 47 | /// the thread local DB field. It should never be called directly. The first time this is called |
| 48 | /// we also call KeystoreDB::cleanup_leftovers to restore the key lifecycle invariant. See the |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 49 | /// documentation of cleanup_leftovers for more details. The function also constructs a blob |
| 50 | /// garbage collector. The initializing closure constructs another database connection without |
| 51 | /// a gc. Although one GC is created for each thread local database connection, this closure |
| 52 | /// is run only once, as long as the ASYNC_TASK instance is the same. So only one additional |
| 53 | /// database connection is created for the garbage collector worker. |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 54 | pub fn create_thread_local_db() -> KeystoreDB { |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 55 | let gc = Gc::new_init_with(ASYNC_TASK.clone(), || { |
| 56 | ( |
| 57 | Box::new(|uuid, blob| { |
| 58 | let km_dev: Strong<dyn IKeyMintDevice> = |
| 59 | get_keymint_dev_by_uuid(uuid).map(|(dev, _)| dev)?.get_interface()?; |
| 60 | map_km_error(km_dev.deleteKey(&*blob)) |
| 61 | .context("In invalidate key closure: Trying to invalidate key blob.") |
| 62 | }), |
| 63 | KeystoreDB::new(&DB_PATH.lock().expect("Could not get the database directory."), None) |
| 64 | .expect("Failed to open database."), |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 65 | SUPER_KEY.clone(), |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 66 | ) |
| 67 | }); |
| 68 | |
| 69 | let mut db = |
| 70 | KeystoreDB::new(&DB_PATH.lock().expect("Could not get the database directory."), Some(gc)) |
| 71 | .expect("Failed to open database."); |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 72 | DB_INIT.call_once(|| { |
| 73 | log::info!("Touching Keystore 2.0 database for this first time since boot."); |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 74 | db.insert_last_off_body(MonotonicRawTime::now()) |
| 75 | .expect("Could not initialize database with last off body."); |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 76 | log::info!("Calling cleanup leftovers."); |
| 77 | let n = db.cleanup_leftovers().expect("Failed to cleanup database on startup."); |
| 78 | if n != 0 { |
| 79 | log::info!( |
| 80 | concat!( |
| 81 | "Cleaned up {} failed entries. ", |
| 82 | "This indicates keystore crashed during key generation." |
| 83 | ), |
| 84 | n |
| 85 | ); |
| 86 | } |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 87 | }); |
| 88 | db |
| 89 | } |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 90 | |
| 91 | thread_local! { |
| 92 | /// Database connections are not thread safe, but connecting to the |
| 93 | /// same database multiple times is safe as long as each connection is |
| 94 | /// used by only one thread. So we store one database connection per |
| 95 | /// thread in this thread local key. |
| 96 | pub static DB: RefCell<KeystoreDB> = |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 97 | RefCell::new(create_thread_local_db()); |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 98 | } |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 99 | |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 100 | #[derive(Default)] |
| 101 | struct DevicesMap { |
| 102 | devices_by_uuid: HashMap<Uuid, (Asp, KeyMintHardwareInfo)>, |
| 103 | uuid_by_sec_level: HashMap<SecurityLevel, Uuid>, |
| 104 | } |
| 105 | |
| 106 | impl DevicesMap { |
| 107 | fn dev_by_sec_level( |
| 108 | &self, |
| 109 | sec_level: &SecurityLevel, |
| 110 | ) -> Option<(Asp, KeyMintHardwareInfo, Uuid)> { |
| 111 | self.uuid_by_sec_level.get(sec_level).and_then(|uuid| self.dev_by_uuid(uuid)) |
| 112 | } |
| 113 | |
| 114 | fn dev_by_uuid(&self, uuid: &Uuid) -> Option<(Asp, KeyMintHardwareInfo, Uuid)> { |
| 115 | self.devices_by_uuid |
| 116 | .get(uuid) |
| 117 | .map(|(dev, hw_info)| ((*dev).clone(), (*hw_info).clone(), *uuid)) |
| 118 | } |
| 119 | |
| 120 | /// The requested security level and the security level of the actual implementation may |
| 121 | /// differ. So we map the requested security level to the uuid of the implementation |
| 122 | /// so that there cannot be any confusion as to which KeyMint instance is requested. |
| 123 | fn insert(&mut self, sec_level: SecurityLevel, dev: Asp, hw_info: KeyMintHardwareInfo) { |
| 124 | // For now we use the reported security level of the KM instance as UUID. |
| 125 | // TODO update this section once UUID was added to the KM hardware info. |
| 126 | let uuid: Uuid = sec_level.into(); |
| 127 | self.devices_by_uuid.insert(uuid, (dev, hw_info)); |
| 128 | self.uuid_by_sec_level.insert(sec_level, uuid); |
| 129 | } |
| 130 | } |
| 131 | |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame] | 132 | #[derive(Default)] |
| 133 | struct RemotelyProvisionedDevicesMap { |
| 134 | devices_by_sec_level: HashMap<SecurityLevel, Asp>, |
| 135 | } |
| 136 | |
| 137 | impl RemotelyProvisionedDevicesMap { |
| 138 | fn dev_by_sec_level(&self, sec_level: &SecurityLevel) -> Option<Asp> { |
| 139 | self.devices_by_sec_level.get(sec_level).map(|dev| (*dev).clone()) |
| 140 | } |
| 141 | |
| 142 | fn insert(&mut self, sec_level: SecurityLevel, dev: Asp) { |
| 143 | self.devices_by_sec_level.insert(sec_level, dev); |
| 144 | } |
| 145 | } |
| 146 | |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 147 | lazy_static! { |
Janis Danisevskis | 3f2955c | 2021-02-02 21:53:35 -0800 | [diff] [blame] | 148 | /// The path where keystore stores all its keys. |
| 149 | pub static ref DB_PATH: Mutex<PathBuf> = Mutex::new( |
| 150 | Path::new("/data/misc/keystore").to_path_buf()); |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 151 | /// Runtime database of unwrapped super keys. |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 152 | pub static ref SUPER_KEY: Arc<SuperKeyManager> = Default::default(); |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 153 | /// Map of KeyMint devices. |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 154 | static ref KEY_MINT_DEVICES: Mutex<DevicesMap> = Default::default(); |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 155 | /// Timestamp service. |
| 156 | static ref TIME_STAMP_DEVICE: Mutex<Option<Asp>> = Default::default(); |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame] | 157 | /// RemotelyProvisionedComponent HAL devices. |
| 158 | static ref REMOTELY_PROVISIONED_COMPONENT_DEVICES: Mutex<RemotelyProvisionedDevicesMap> = Default::default(); |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 159 | /// A single on-demand worker thread that handles deferred tasks with two different |
| 160 | /// priorities. |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 161 | pub static ref ASYNC_TASK: Arc<AsyncTask> = Default::default(); |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 162 | /// Singleton for enforcements. |
Paul Crowley | 7c57bf1 | 2021-02-02 16:26:57 -0800 | [diff] [blame^] | 163 | pub static ref ENFORCEMENTS: Enforcements = Default::default(); |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 164 | /// LegacyBlobLoader is initialized and exists globally. |
| 165 | /// The same directory used by the database is used by the LegacyBlobLoader as well. |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 166 | pub static ref LEGACY_BLOB_LOADER: Arc<LegacyBlobLoader> = Arc::new(LegacyBlobLoader::new( |
| 167 | &DB_PATH.lock().expect("Could not get the database path for legacy blob loader."))); |
| 168 | /// Legacy migrator. Atomically migrates legacy blobs to the database. |
| 169 | pub static ref LEGACY_MIGRATOR: Arc<LegacyMigrator> = |
| 170 | Arc::new(LegacyMigrator::new(ASYNC_TASK.clone())); |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static KEYMINT_SERVICE_NAME: &str = "android.hardware.security.keymint.IKeyMintDevice"; |
| 174 | |
| 175 | /// Make a new connection to a KeyMint device of the given security level. |
Janis Danisevskis | 8c6378e | 2021-01-01 09:30:37 -0800 | [diff] [blame] | 176 | /// If no native KeyMint device can be found this function also brings |
| 177 | /// up the compatibility service and attempts to connect to the legacy wrapper. |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 178 | fn connect_keymint(security_level: &SecurityLevel) -> Result<(Asp, KeyMintHardwareInfo)> { |
Janis Danisevskis | ef14e1a | 2021-02-23 23:16:55 -0800 | [diff] [blame] | 179 | let keymint_instances = |
| 180 | get_aidl_instances("android.hardware.security.keymint", 1, "IKeyMintDevice"); |
| 181 | |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 182 | let service_name = match *security_level { |
Janis Danisevskis | ef14e1a | 2021-02-23 23:16:55 -0800 | [diff] [blame] | 183 | SecurityLevel::TRUSTED_ENVIRONMENT => { |
| 184 | if keymint_instances.as_vec()?.iter().any(|instance| *instance == "default") { |
| 185 | Some(format!("{}/default", KEYMINT_SERVICE_NAME)) |
| 186 | } else { |
| 187 | None |
| 188 | } |
| 189 | } |
| 190 | SecurityLevel::STRONGBOX => { |
| 191 | if keymint_instances.as_vec()?.iter().any(|instance| *instance == "strongbox") { |
| 192 | Some(format!("{}/strongbox", KEYMINT_SERVICE_NAME)) |
| 193 | } else { |
| 194 | None |
| 195 | } |
| 196 | } |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 197 | _ => { |
| 198 | return Err(Error::Km(ErrorCode::HARDWARE_TYPE_UNAVAILABLE)) |
| 199 | .context("In connect_keymint.") |
| 200 | } |
| 201 | }; |
| 202 | |
Janis Danisevskis | ef14e1a | 2021-02-23 23:16:55 -0800 | [diff] [blame] | 203 | let keymint = if let Some(service_name) = service_name { |
| 204 | map_binder_status_code(binder::get_interface(&service_name)) |
| 205 | .context("In connect_keymint: Trying to connect to genuine KeyMint service.") |
| 206 | } else { |
| 207 | // This is a no-op if it was called before. |
| 208 | keystore2_km_compat::add_keymint_device_service(); |
Janis Danisevskis | 8c6378e | 2021-01-01 09:30:37 -0800 | [diff] [blame] | 209 | |
Janis Danisevskis | ef14e1a | 2021-02-23 23:16:55 -0800 | [diff] [blame] | 210 | let keystore_compat_service: Strong<dyn IKeystoreCompatService> = |
| 211 | map_binder_status_code(binder::get_interface("android.security.compat")) |
| 212 | .context("In connect_keymint: Trying to connect to compat service.")?; |
| 213 | map_binder_status(keystore_compat_service.getKeyMintDevice(*security_level)) |
| 214 | .map_err(|e| match e { |
| 215 | Error::BinderTransaction(StatusCode::NAME_NOT_FOUND) => { |
| 216 | Error::Km(ErrorCode::HARDWARE_TYPE_UNAVAILABLE) |
Janis Danisevskis | 8c6378e | 2021-01-01 09:30:37 -0800 | [diff] [blame] | 217 | } |
Janis Danisevskis | ef14e1a | 2021-02-23 23:16:55 -0800 | [diff] [blame] | 218 | e => e, |
| 219 | }) |
| 220 | .context("In connect_keymint: Trying to get Legacy wrapper.") |
| 221 | }?; |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 222 | |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 223 | let hw_info = map_km_error(keymint.getHardwareInfo()) |
| 224 | .context("In connect_keymint: Failed to get hardware info.")?; |
| 225 | |
| 226 | Ok((Asp::new(keymint.as_binder()), hw_info)) |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /// Get a keymint device for the given security level either from our cache or |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 230 | /// by making a new connection. Returns the device, the hardware info and the uuid. |
| 231 | /// TODO the latter can be removed when the uuid is part of the hardware info. |
| 232 | pub fn get_keymint_device( |
| 233 | security_level: &SecurityLevel, |
| 234 | ) -> Result<(Asp, KeyMintHardwareInfo, Uuid)> { |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 235 | let mut devices_map = KEY_MINT_DEVICES.lock().unwrap(); |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 236 | if let Some((dev, hw_info, uuid)) = devices_map.dev_by_sec_level(&security_level) { |
| 237 | Ok((dev, hw_info, uuid)) |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 238 | } else { |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 239 | let (dev, hw_info) = connect_keymint(security_level).context("In get_keymint_device.")?; |
| 240 | devices_map.insert(*security_level, dev, hw_info); |
| 241 | // Unwrap must succeed because we just inserted it. |
| 242 | Ok(devices_map.dev_by_sec_level(security_level).unwrap()) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /// Get a keymint device for the given uuid. This will only access the cache, but will not |
| 247 | /// attempt to establish a new connection. It is assumed that the cache is already populated |
| 248 | /// when this is called. This is a fair assumption, because service.rs iterates through all |
| 249 | /// security levels when it gets instantiated. |
| 250 | pub fn get_keymint_dev_by_uuid(uuid: &Uuid) -> Result<(Asp, KeyMintHardwareInfo)> { |
| 251 | let devices_map = KEY_MINT_DEVICES.lock().unwrap(); |
| 252 | if let Some((dev, hw_info, _)) = devices_map.dev_by_uuid(uuid) { |
| 253 | Ok((dev, hw_info)) |
| 254 | } else { |
| 255 | Err(Error::sys()).context("In get_keymint_dev_by_uuid: No KeyMint instance found.") |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 256 | } |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 257 | } |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 258 | |
| 259 | static TIME_STAMP_SERVICE_NAME: &str = "android.hardware.security.secureclock.ISecureClock"; |
| 260 | |
| 261 | /// Make a new connection to a secure clock service. |
| 262 | /// If no native SecureClock device can be found brings up the compatibility service and attempts |
| 263 | /// to connect to the legacy wrapper. |
| 264 | fn connect_secureclock() -> Result<Asp> { |
Janis Danisevskis | ef14e1a | 2021-02-23 23:16:55 -0800 | [diff] [blame] | 265 | let secureclock_instances = |
| 266 | get_aidl_instances("android.hardware.security.secureclock", 1, "ISecureClock"); |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 267 | |
Janis Danisevskis | ef14e1a | 2021-02-23 23:16:55 -0800 | [diff] [blame] | 268 | let secure_clock_available = |
| 269 | secureclock_instances.as_vec()?.iter().any(|instance| *instance == "default"); |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 270 | |
Janis Danisevskis | ef14e1a | 2021-02-23 23:16:55 -0800 | [diff] [blame] | 271 | let secureclock = if secure_clock_available { |
| 272 | map_binder_status_code(binder::get_interface(TIME_STAMP_SERVICE_NAME)) |
| 273 | .context("In connect_secureclock: Trying to connect to genuine secure clock service.") |
| 274 | } else { |
| 275 | // This is a no-op if it was called before. |
| 276 | keystore2_km_compat::add_keymint_device_service(); |
| 277 | |
| 278 | let keystore_compat_service: Strong<dyn IKeystoreCompatService> = |
| 279 | map_binder_status_code(binder::get_interface("android.security.compat")) |
| 280 | .context("In connect_secureclock: Trying to connect to compat service.")?; |
| 281 | |
| 282 | // Legacy secure clock services were only implemented by TEE. |
| 283 | map_binder_status(keystore_compat_service.getSecureClock()) |
| 284 | .map_err(|e| match e { |
| 285 | Error::BinderTransaction(StatusCode::NAME_NOT_FOUND) => { |
| 286 | Error::Km(ErrorCode::HARDWARE_TYPE_UNAVAILABLE) |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 287 | } |
Janis Danisevskis | ef14e1a | 2021-02-23 23:16:55 -0800 | [diff] [blame] | 288 | e => e, |
| 289 | }) |
| 290 | .context("In connect_secureclock: Trying to get Legacy wrapper.") |
| 291 | }?; |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 292 | |
| 293 | Ok(Asp::new(secureclock.as_binder())) |
| 294 | } |
| 295 | |
| 296 | /// Get the timestamp service that verifies auth token timeliness towards security levels with |
| 297 | /// different clocks. |
| 298 | pub fn get_timestamp_service() -> Result<Asp> { |
| 299 | let mut ts_device = TIME_STAMP_DEVICE.lock().unwrap(); |
| 300 | if let Some(dev) = &*ts_device { |
| 301 | Ok(dev.clone()) |
| 302 | } else { |
| 303 | let dev = connect_secureclock().context("In get_timestamp_service.")?; |
| 304 | *ts_device = Some(dev.clone()); |
| 305 | Ok(dev) |
| 306 | } |
| 307 | } |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame] | 308 | |
| 309 | static REMOTE_PROVISIONING_HAL_SERVICE_NAME: &str = |
| 310 | "android.hardware.security.keymint.IRemotelyProvisionedComponent"; |
| 311 | |
| 312 | fn connect_remotely_provisioned_component(security_level: &SecurityLevel) -> Result<Asp> { |
Janis Danisevskis | ef14e1a | 2021-02-23 23:16:55 -0800 | [diff] [blame] | 313 | let remotely_prov_instances = |
| 314 | get_aidl_instances("android.hardware.security.keymint", 1, "IRemotelyProvisionedComponent"); |
| 315 | |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame] | 316 | let service_name = match *security_level { |
| 317 | SecurityLevel::TRUSTED_ENVIRONMENT => { |
Janis Danisevskis | ef14e1a | 2021-02-23 23:16:55 -0800 | [diff] [blame] | 318 | if remotely_prov_instances.as_vec()?.iter().any(|instance| *instance == "default") { |
| 319 | Some(format!("{}/default", REMOTE_PROVISIONING_HAL_SERVICE_NAME)) |
| 320 | } else { |
| 321 | None |
| 322 | } |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame] | 323 | } |
Janis Danisevskis | ef14e1a | 2021-02-23 23:16:55 -0800 | [diff] [blame] | 324 | SecurityLevel::STRONGBOX => { |
| 325 | if remotely_prov_instances.as_vec()?.iter().any(|instance| *instance == "strongbox") { |
| 326 | Some(format!("{}/strongbox", REMOTE_PROVISIONING_HAL_SERVICE_NAME)) |
| 327 | } else { |
| 328 | None |
| 329 | } |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame] | 330 | } |
Janis Danisevskis | ef14e1a | 2021-02-23 23:16:55 -0800 | [diff] [blame] | 331 | _ => None, |
| 332 | } |
| 333 | .ok_or(Error::Km(ErrorCode::HARDWARE_TYPE_UNAVAILABLE)) |
| 334 | .context("In connect_remotely_provisioned_component.")?; |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame] | 335 | |
| 336 | let rem_prov_hal: Strong<dyn IRemotelyProvisionedComponent> = |
| 337 | map_binder_status_code(binder::get_interface(&service_name)) |
| 338 | .context(concat!( |
| 339 | "In connect_remotely_provisioned_component: Trying to connect to", |
| 340 | " RemotelyProvisionedComponent service." |
| 341 | )) |
| 342 | .map_err(|e| e)?; |
| 343 | Ok(Asp::new(rem_prov_hal.as_binder())) |
| 344 | } |
| 345 | |
| 346 | /// Get a remote provisiong component device for the given security level either from the cache or |
| 347 | /// by making a new connection. Returns the device. |
| 348 | pub fn get_remotely_provisioned_component(security_level: &SecurityLevel) -> Result<Asp> { |
| 349 | let mut devices_map = REMOTELY_PROVISIONED_COMPONENT_DEVICES.lock().unwrap(); |
| 350 | if let Some(dev) = devices_map.dev_by_sec_level(&security_level) { |
| 351 | Ok(dev) |
| 352 | } else { |
| 353 | let dev = connect_remotely_provisioned_component(security_level) |
| 354 | .context("In get_remotely_provisioned_component.")?; |
| 355 | devices_map.insert(*security_level, dev); |
| 356 | // Unwrap must succeed because we just inserted it. |
| 357 | Ok(devices_map.dev_by_sec_level(security_level).unwrap()) |
| 358 | } |
| 359 | } |