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