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 | e242b03 | 2020-11-17 15:57:51 -0800 | [diff] [blame] | 15 | //! This crate implements the Keystore 2.0 service entry point. |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 16 | |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 17 | use keystore2::entropy; |
Janis Danisevskis | b1673db | 2021-02-08 18:11:57 -0800 | [diff] [blame] | 18 | use keystore2::globals::ENFORCEMENTS; |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 19 | use keystore2::maintenance::Maintenance; |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 20 | use keystore2::metrics::Metrics; |
Hasini Gunasinghe | 365ce37 | 2021-07-02 23:13:11 +0000 | [diff] [blame] | 21 | use keystore2::metrics_store; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 22 | use keystore2::service::KeystoreService; |
Janis Danisevskis | 84a83e4 | 2021-03-21 21:46:54 -0700 | [diff] [blame] | 23 | use keystore2::{apc::ApcManager, shared_secret_negotiation}; |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 24 | use keystore2::{authorization::AuthorizationManager, id_rotation::IdRotationState}; |
Janis Danisevskis | 3eb829d | 2021-06-14 14:18:20 -0700 | [diff] [blame] | 25 | use legacykeystore::LegacyKeystore; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 26 | use log::{error, info}; |
Seth Moore | e3fd587 | 2021-08-17 13:21:09 -0700 | [diff] [blame] | 27 | use rusqlite::trace as sqlite_trace; |
| 28 | use std::{os::raw::c_int, panic, path::Path, sync::mpsc::channel}; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 29 | |
Stephen Crane | 56936e8 | 2021-03-10 17:48:26 -0800 | [diff] [blame] | 30 | static KS2_SERVICE_NAME: &str = "android.system.keystore2.IKeystoreService/default"; |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 31 | static APC_SERVICE_NAME: &str = "android.security.apc"; |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 32 | static AUTHORIZATION_SERVICE_NAME: &str = "android.security.authorization"; |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 33 | static METRICS_SERVICE_NAME: &str = "android.security.metrics"; |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 34 | static USER_MANAGER_SERVICE_NAME: &str = "android.security.maintenance"; |
Janis Danisevskis | 3eb829d | 2021-06-14 14:18:20 -0700 | [diff] [blame] | 35 | static LEGACY_KEYSTORE_SERVICE_NAME: &str = "android.security.legacykeystore"; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 36 | |
| 37 | /// Keystore 2.0 takes one argument which is a path indicating its designated working directory. |
| 38 | fn main() { |
| 39 | // Initialize android logging. |
| 40 | android_logger::init_once( |
Matthew Maurer | 1f49c6e | 2022-02-17 20:29:07 +0000 | [diff] [blame] | 41 | android_logger::Config::default() |
| 42 | .with_tag("keystore2") |
| 43 | .with_min_level(log::Level::Debug) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 44 | .with_log_id(android_logger::LogId::System) |
| 45 | .format(|buf, record| { |
| 46 | writeln!( |
| 47 | buf, |
| 48 | "{}:{} - {}", |
| 49 | record.file().unwrap_or("unknown"), |
| 50 | record.line().unwrap_or(0), |
| 51 | record.args() |
| 52 | ) |
| 53 | }), |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 54 | ); |
| 55 | // Redirect panic messages to logcat. |
| 56 | panic::set_hook(Box::new(|panic_info| { |
| 57 | error!("{}", panic_info); |
| 58 | })); |
| 59 | |
| 60 | // Saying hi. |
| 61 | info!("Keystore2 is starting."); |
| 62 | |
| 63 | let mut args = std::env::args(); |
| 64 | args.next().expect("That's odd. How is there not even a first argument?"); |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 65 | |
Seth Moore | e3fd587 | 2021-08-17 13:21:09 -0700 | [diff] [blame] | 66 | // This must happen early before any other sqlite operations. |
| 67 | log::info!("Setting up sqlite logging for keystore2"); |
| 68 | fn sqlite_log_handler(err: c_int, message: &str) { |
| 69 | log::error!("[SQLITE3] {}: {}", err, message); |
| 70 | } |
Andrew Walbran | a47698a | 2023-07-21 17:23:56 +0100 | [diff] [blame] | 71 | // SAFETY: There are no other threads yet, `sqlite_log_handler` is threadsafe, and it doesn't |
| 72 | // invoke any SQLite calls. |
Seth Moore | e3fd587 | 2021-08-17 13:21:09 -0700 | [diff] [blame] | 73 | unsafe { sqlite_trace::config_log(Some(sqlite_log_handler)) } |
| 74 | .expect("Error setting sqlite log callback."); |
| 75 | |
Hasini Gunasinghe | 365ce37 | 2021-07-02 23:13:11 +0000 | [diff] [blame] | 76 | // Write/update keystore.crash_count system property. |
| 77 | metrics_store::update_keystore_crash_sysprop(); |
| 78 | |
Janis Danisevskis | 3f2955c | 2021-02-02 21:53:35 -0800 | [diff] [blame] | 79 | // Keystore 2.0 cannot change to the database directory (typically /data/misc/keystore) on |
| 80 | // startup as Keystore 1.0 did because Keystore 2.0 is intended to run much earlier than |
| 81 | // Keystore 1.0. Instead we set a global variable to the database path. |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 82 | // For the ground truth check the service startup rule for init (typically in keystore2.rc). |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 83 | let id_rotation_state = if let Some(dir) = args.next() { |
| 84 | let db_path = Path::new(&dir); |
Seth Moore | a3e611a | 2021-05-11 10:07:45 -0700 | [diff] [blame] | 85 | *keystore2::globals::DB_PATH.write().expect("Could not lock DB_PATH.") = |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 86 | db_path.to_path_buf(); |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 87 | IdRotationState::new(db_path) |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 88 | } else { |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 89 | panic!("Must specify a database directory."); |
| 90 | }; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 91 | |
Janis Danisevskis | b1673db | 2021-02-08 18:11:57 -0800 | [diff] [blame] | 92 | let (confirmation_token_sender, confirmation_token_receiver) = channel(); |
| 93 | |
| 94 | ENFORCEMENTS.install_confirmation_token_receiver(confirmation_token_receiver); |
| 95 | |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 96 | entropy::register_feeder(); |
Janis Danisevskis | 84a83e4 | 2021-03-21 21:46:54 -0700 | [diff] [blame] | 97 | shared_secret_negotiation::perform_shared_secret_negotiation(); |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 98 | |
Janis Danisevskis | 8c6378e | 2021-01-01 09:30:37 -0800 | [diff] [blame] | 99 | info!("Starting thread pool now."); |
| 100 | binder::ProcessState::start_thread_pool(); |
| 101 | |
Janis Danisevskis | 5cb52dc | 2021-04-07 16:31:18 -0700 | [diff] [blame] | 102 | let ks_service = KeystoreService::new_native_binder(id_rotation_state).unwrap_or_else(|e| { |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 103 | panic!("Failed to create service {} because of {:?}.", KS2_SERVICE_NAME, e); |
| 104 | }); |
| 105 | binder::add_service(KS2_SERVICE_NAME, ks_service.as_binder()).unwrap_or_else(|e| { |
| 106 | panic!("Failed to register service {} because of {:?}.", KS2_SERVICE_NAME, e); |
| 107 | }); |
| 108 | |
Janis Danisevskis | b1673db | 2021-02-08 18:11:57 -0800 | [diff] [blame] | 109 | let apc_service = |
| 110 | ApcManager::new_native_binder(confirmation_token_sender).unwrap_or_else(|e| { |
| 111 | panic!("Failed to create service {} because of {:?}.", APC_SERVICE_NAME, e); |
| 112 | }); |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 113 | binder::add_service(APC_SERVICE_NAME, apc_service.as_binder()).unwrap_or_else(|e| { |
| 114 | panic!("Failed to register service {} because of {:?}.", APC_SERVICE_NAME, e); |
| 115 | }); |
| 116 | |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 117 | let authorization_service = AuthorizationManager::new_native_binder().unwrap_or_else(|e| { |
| 118 | panic!("Failed to create service {} because of {:?}.", AUTHORIZATION_SERVICE_NAME, e); |
| 119 | }); |
| 120 | binder::add_service(AUTHORIZATION_SERVICE_NAME, authorization_service.as_binder()) |
| 121 | .unwrap_or_else(|e| { |
| 122 | panic!("Failed to register service {} because of {:?}.", AUTHORIZATION_SERVICE_NAME, e); |
| 123 | }); |
| 124 | |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 125 | let (delete_listener, legacykeystore) = LegacyKeystore::new_native_binder( |
| 126 | &keystore2::globals::DB_PATH.read().expect("Could not get DB_PATH."), |
| 127 | ); |
| 128 | |
| 129 | let maintenance_service = Maintenance::new_native_binder(delete_listener).unwrap_or_else(|e| { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 130 | panic!("Failed to create service {} because of {:?}.", USER_MANAGER_SERVICE_NAME, e); |
| 131 | }); |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 132 | binder::add_service(USER_MANAGER_SERVICE_NAME, maintenance_service.as_binder()).unwrap_or_else( |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 133 | |e| { |
| 134 | panic!("Failed to register service {} because of {:?}.", USER_MANAGER_SERVICE_NAME, e); |
| 135 | }, |
| 136 | ); |
| 137 | |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 138 | let metrics_service = Metrics::new_native_binder().unwrap_or_else(|e| { |
| 139 | panic!("Failed to create service {} because of {:?}.", METRICS_SERVICE_NAME, e); |
| 140 | }); |
| 141 | binder::add_service(METRICS_SERVICE_NAME, metrics_service.as_binder()).unwrap_or_else(|e| { |
| 142 | panic!("Failed to register service {} because of {:?}.", METRICS_SERVICE_NAME, e); |
| 143 | }); |
| 144 | |
Janis Danisevskis | 3eb829d | 2021-06-14 14:18:20 -0700 | [diff] [blame] | 145 | binder::add_service(LEGACY_KEYSTORE_SERVICE_NAME, legacykeystore.as_binder()).unwrap_or_else( |
Janis Danisevskis | 77d7204 | 2021-01-20 15:36:30 -0800 | [diff] [blame] | 146 | |e| { |
| 147 | panic!( |
| 148 | "Failed to register service {} because of {:?}.", |
Janis Danisevskis | 3eb829d | 2021-06-14 14:18:20 -0700 | [diff] [blame] | 149 | LEGACY_KEYSTORE_SERVICE_NAME, e |
Janis Danisevskis | 77d7204 | 2021-01-20 15:36:30 -0800 | [diff] [blame] | 150 | ); |
| 151 | }, |
| 152 | ); |
| 153 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 154 | info!("Successfully registered Keystore 2.0 service."); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 155 | |
Janis Danisevskis | e242b03 | 2020-11-17 15:57:51 -0800 | [diff] [blame] | 156 | info!("Joining thread pool now."); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 157 | binder::ProcessState::join_thread_pool(); |
| 158 | } |