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 | |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 17 | use keystore2::authorization::AuthorizationManager; |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 18 | use keystore2::entropy; |
Janis Danisevskis | b1673db | 2021-02-08 18:11:57 -0800 | [diff] [blame] | 19 | use keystore2::globals::ENFORCEMENTS; |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 20 | use keystore2::maintenance::Maintenance; |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame] | 21 | use keystore2::remote_provisioning::RemoteProvisioningService; |
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 | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 24 | use log::{error, info}; |
Janis Danisevskis | b1673db | 2021-02-08 18:11:57 -0800 | [diff] [blame] | 25 | use std::{panic, path::Path, sync::mpsc::channel}; |
Janis Danisevskis | 77d7204 | 2021-01-20 15:36:30 -0800 | [diff] [blame] | 26 | use vpnprofilestore::VpnProfileStore; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 27 | |
Stephen Crane | 56936e8 | 2021-03-10 17:48:26 -0800 | [diff] [blame] | 28 | static KS2_SERVICE_NAME: &str = "android.system.keystore2.IKeystoreService/default"; |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 29 | static APC_SERVICE_NAME: &str = "android.security.apc"; |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 30 | static AUTHORIZATION_SERVICE_NAME: &str = "android.security.authorization"; |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame] | 31 | static REMOTE_PROVISIONING_SERVICE_NAME: &str = "android.security.remoteprovisioning"; |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 32 | static USER_MANAGER_SERVICE_NAME: &str = "android.security.maintenance"; |
Janis Danisevskis | 77d7204 | 2021-01-20 15:36:30 -0800 | [diff] [blame] | 33 | static VPNPROFILESTORE_SERVICE_NAME: &str = "android.security.vpnprofilestore"; |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 34 | |
| 35 | /// Keystore 2.0 takes one argument which is a path indicating its designated working directory. |
| 36 | fn main() { |
| 37 | // Initialize android logging. |
| 38 | android_logger::init_once( |
| 39 | android_logger::Config::default().with_tag("keystore2").with_min_level(log::Level::Debug), |
| 40 | ); |
| 41 | // Redirect panic messages to logcat. |
| 42 | panic::set_hook(Box::new(|panic_info| { |
| 43 | error!("{}", panic_info); |
| 44 | })); |
| 45 | |
| 46 | // Saying hi. |
| 47 | info!("Keystore2 is starting."); |
| 48 | |
Janis Danisevskis | b00ebd0 | 2021-02-02 21:52:24 -0800 | [diff] [blame] | 49 | // Initialize the per boot database. |
| 50 | let _keep_me_alive = keystore2::database::KeystoreDB::keep_perboot_db_alive() |
| 51 | .expect("Failed to initialize the perboot database."); |
| 52 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 53 | let mut args = std::env::args(); |
| 54 | 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] | 55 | |
Janis Danisevskis | 3f2955c | 2021-02-02 21:53:35 -0800 | [diff] [blame] | 56 | // Keystore 2.0 cannot change to the database directory (typically /data/misc/keystore) on |
| 57 | // startup as Keystore 1.0 did because Keystore 2.0 is intended to run much earlier than |
| 58 | // 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] | 59 | // For the ground truth check the service startup rule for init (typically in keystore2.rc). |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 60 | if let Some(dir) = args.next() { |
Janis Danisevskis | 3f2955c | 2021-02-02 21:53:35 -0800 | [diff] [blame] | 61 | *keystore2::globals::DB_PATH.lock().expect("Could not lock DB_PATH.") = |
| 62 | Path::new(&dir).to_path_buf(); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 63 | } else { |
| 64 | panic!("Must specify a working directory."); |
| 65 | } |
| 66 | |
Janis Danisevskis | b1673db | 2021-02-08 18:11:57 -0800 | [diff] [blame] | 67 | let (confirmation_token_sender, confirmation_token_receiver) = channel(); |
| 68 | |
| 69 | ENFORCEMENTS.install_confirmation_token_receiver(confirmation_token_receiver); |
| 70 | |
Ulyana Trafimovich | 229f2c0 | 2021-04-06 16:07:07 +0000 | [diff] [blame] | 71 | info!("Starting boot level watcher."); |
| 72 | std::thread::spawn(|| { |
| 73 | keystore2::globals::ENFORCEMENTS |
| 74 | .watch_boot_level() |
| 75 | .unwrap_or_else(|e| error!("watch_boot_level failed: {}", e)); |
| 76 | }); |
| 77 | |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 78 | entropy::register_feeder(); |
Janis Danisevskis | 84a83e4 | 2021-03-21 21:46:54 -0700 | [diff] [blame] | 79 | shared_secret_negotiation::perform_shared_secret_negotiation(); |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 80 | |
Janis Danisevskis | 8c6378e | 2021-01-01 09:30:37 -0800 | [diff] [blame] | 81 | info!("Starting thread pool now."); |
| 82 | binder::ProcessState::start_thread_pool(); |
| 83 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 84 | let ks_service = KeystoreService::new_native_binder().unwrap_or_else(|e| { |
| 85 | panic!("Failed to create service {} because of {:?}.", KS2_SERVICE_NAME, e); |
| 86 | }); |
| 87 | binder::add_service(KS2_SERVICE_NAME, ks_service.as_binder()).unwrap_or_else(|e| { |
| 88 | panic!("Failed to register service {} because of {:?}.", KS2_SERVICE_NAME, e); |
| 89 | }); |
| 90 | |
Janis Danisevskis | b1673db | 2021-02-08 18:11:57 -0800 | [diff] [blame] | 91 | let apc_service = |
| 92 | ApcManager::new_native_binder(confirmation_token_sender).unwrap_or_else(|e| { |
| 93 | panic!("Failed to create service {} because of {:?}.", APC_SERVICE_NAME, e); |
| 94 | }); |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 95 | binder::add_service(APC_SERVICE_NAME, apc_service.as_binder()).unwrap_or_else(|e| { |
| 96 | panic!("Failed to register service {} because of {:?}.", APC_SERVICE_NAME, e); |
| 97 | }); |
| 98 | |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 99 | let authorization_service = AuthorizationManager::new_native_binder().unwrap_or_else(|e| { |
| 100 | panic!("Failed to create service {} because of {:?}.", AUTHORIZATION_SERVICE_NAME, e); |
| 101 | }); |
| 102 | binder::add_service(AUTHORIZATION_SERVICE_NAME, authorization_service.as_binder()) |
| 103 | .unwrap_or_else(|e| { |
| 104 | panic!("Failed to register service {} because of {:?}.", AUTHORIZATION_SERVICE_NAME, e); |
| 105 | }); |
| 106 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 107 | let maintenance_service = Maintenance::new_native_binder().unwrap_or_else(|e| { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 108 | panic!("Failed to create service {} because of {:?}.", USER_MANAGER_SERVICE_NAME, e); |
| 109 | }); |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 110 | 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] | 111 | |e| { |
| 112 | panic!("Failed to register service {} because of {:?}.", USER_MANAGER_SERVICE_NAME, e); |
| 113 | }, |
| 114 | ); |
| 115 | |
Max Bires | b2e1d03 | 2021-02-08 21:35:05 -0800 | [diff] [blame] | 116 | // Devices with KS2 and KM 1.0 may not have any IRemotelyProvisionedComponent HALs at all. Do |
| 117 | // not panic if new_native_binder returns failure because it could not find the TEE HAL. |
| 118 | if let Ok(remote_provisioning_service) = RemoteProvisioningService::new_native_binder() { |
| 119 | binder::add_service( |
| 120 | REMOTE_PROVISIONING_SERVICE_NAME, |
| 121 | remote_provisioning_service.as_binder(), |
| 122 | ) |
| 123 | .unwrap_or_else(|e| { |
| 124 | panic!( |
| 125 | "Failed to register service {} because of {:?}.", |
| 126 | REMOTE_PROVISIONING_SERVICE_NAME, e |
| 127 | ); |
| 128 | }); |
| 129 | } |
Janis Danisevskis | 77d7204 | 2021-01-20 15:36:30 -0800 | [diff] [blame] | 130 | |
| 131 | let vpnprofilestore = VpnProfileStore::new_native_binder( |
| 132 | &keystore2::globals::DB_PATH.lock().expect("Could not get DB_PATH."), |
| 133 | ); |
| 134 | binder::add_service(VPNPROFILESTORE_SERVICE_NAME, vpnprofilestore.as_binder()).unwrap_or_else( |
| 135 | |e| { |
| 136 | panic!( |
| 137 | "Failed to register service {} because of {:?}.", |
| 138 | VPNPROFILESTORE_SERVICE_NAME, e |
| 139 | ); |
| 140 | }, |
| 141 | ); |
| 142 | |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 143 | info!("Successfully registered Keystore 2.0 service."); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 144 | |
Janis Danisevskis | e242b03 | 2020-11-17 15:57:51 -0800 | [diff] [blame] | 145 | info!("Joining thread pool now."); |
Janis Danisevskis | 1af9126 | 2020-08-10 14:58:08 -0700 | [diff] [blame] | 146 | binder::ProcessState::join_thread_pool(); |
| 147 | } |