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