blob: 53461da66d0071e42e7e3a817096a84fa16b4a8f [file] [log] [blame]
Janis Danisevskis1af91262020-08-10 14:58:08 -07001// 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 Danisevskise242b032020-11-17 15:57:51 -080015//! This crate implements the Keystore 2.0 service entry point.
Janis Danisevskis1af91262020-08-10 14:58:08 -070016
David Drysdale0e45a612021-02-25 17:24:36 +000017use keystore2::entropy;
Janis Danisevskisb1673db2021-02-08 18:11:57 -080018use keystore2::globals::ENFORCEMENTS;
Janis Danisevskis333b7c02021-03-23 18:57:41 -070019use keystore2::maintenance::Maintenance;
Seth Moore78c091f2021-04-09 21:38:30 +000020use keystore2::metrics;
Max Biresb2e1d032021-02-08 21:35:05 -080021use keystore2::remote_provisioning::RemoteProvisioningService;
Janis Danisevskis1af91262020-08-10 14:58:08 -070022use keystore2::service::KeystoreService;
Janis Danisevskis84a83e42021-03-21 21:46:54 -070023use keystore2::{apc::ApcManager, shared_secret_negotiation};
Janis Danisevskis5cb52dc2021-04-07 16:31:18 -070024use keystore2::{authorization::AuthorizationManager, id_rotation::IdRotationState};
Janis Danisevskis1af91262020-08-10 14:58:08 -070025use log::{error, info};
Janis Danisevskisb1673db2021-02-08 18:11:57 -080026use std::{panic, path::Path, sync::mpsc::channel};
Janis Danisevskis77d72042021-01-20 15:36:30 -080027use vpnprofilestore::VpnProfileStore;
Janis Danisevskis1af91262020-08-10 14:58:08 -070028
Stephen Crane56936e82021-03-10 17:48:26 -080029static KS2_SERVICE_NAME: &str = "android.system.keystore2.IKeystoreService/default";
Janis Danisevskis7a1cf382020-11-20 11:22:14 -080030static APC_SERVICE_NAME: &str = "android.security.apc";
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000031static AUTHORIZATION_SERVICE_NAME: &str = "android.security.authorization";
Max Biresb2e1d032021-02-08 21:35:05 -080032static REMOTE_PROVISIONING_SERVICE_NAME: &str = "android.security.remoteprovisioning";
Janis Danisevskis34a0cf22021-03-08 09:19:03 -080033static USER_MANAGER_SERVICE_NAME: &str = "android.security.maintenance";
Janis Danisevskis77d72042021-01-20 15:36:30 -080034static VPNPROFILESTORE_SERVICE_NAME: &str = "android.security.vpnprofilestore";
Janis Danisevskis1af91262020-08-10 14:58:08 -070035
36/// Keystore 2.0 takes one argument which is a path indicating its designated working directory.
37fn main() {
38 // Initialize android logging.
39 android_logger::init_once(
40 android_logger::Config::default().with_tag("keystore2").with_min_level(log::Level::Debug),
41 );
42 // Redirect panic messages to logcat.
43 panic::set_hook(Box::new(|panic_info| {
44 error!("{}", panic_info);
45 }));
46
47 // Saying hi.
48 info!("Keystore2 is starting.");
49
50 let mut args = std::env::args();
51 args.next().expect("That's odd. How is there not even a first argument?");
Janis Danisevskisbf15d732020-12-08 10:35:26 -080052
Janis Danisevskis3f2955c2021-02-02 21:53:35 -080053 // Keystore 2.0 cannot change to the database directory (typically /data/misc/keystore) on
54 // startup as Keystore 1.0 did because Keystore 2.0 is intended to run much earlier than
55 // Keystore 1.0. Instead we set a global variable to the database path.
Janis Danisevskisbf15d732020-12-08 10:35:26 -080056 // For the ground truth check the service startup rule for init (typically in keystore2.rc).
Janis Danisevskis5cb52dc2021-04-07 16:31:18 -070057 let id_rotation_state = if let Some(dir) = args.next() {
58 let db_path = Path::new(&dir);
Seth Moorea3e611a2021-05-11 10:07:45 -070059 *keystore2::globals::DB_PATH.write().expect("Could not lock DB_PATH.") =
Janis Danisevskis5cb52dc2021-04-07 16:31:18 -070060 db_path.to_path_buf();
61 IdRotationState::new(&db_path)
Janis Danisevskis1af91262020-08-10 14:58:08 -070062 } else {
Janis Danisevskis5cb52dc2021-04-07 16:31:18 -070063 panic!("Must specify a database directory.");
64 };
Janis Danisevskis1af91262020-08-10 14:58:08 -070065
Janis Danisevskisb1673db2021-02-08 18:11:57 -080066 let (confirmation_token_sender, confirmation_token_receiver) = channel();
67
68 ENFORCEMENTS.install_confirmation_token_receiver(confirmation_token_receiver);
69
David Drysdale0e45a612021-02-25 17:24:36 +000070 entropy::register_feeder();
Janis Danisevskis84a83e42021-03-21 21:46:54 -070071 shared_secret_negotiation::perform_shared_secret_negotiation();
David Drysdale0e45a612021-02-25 17:24:36 +000072
Janis Danisevskis8c6378e2021-01-01 09:30:37 -080073 info!("Starting thread pool now.");
74 binder::ProcessState::start_thread_pool();
75
Janis Danisevskis5cb52dc2021-04-07 16:31:18 -070076 let ks_service = KeystoreService::new_native_binder(id_rotation_state).unwrap_or_else(|e| {
Janis Danisevskis1af91262020-08-10 14:58:08 -070077 panic!("Failed to create service {} because of {:?}.", KS2_SERVICE_NAME, e);
78 });
79 binder::add_service(KS2_SERVICE_NAME, ks_service.as_binder()).unwrap_or_else(|e| {
80 panic!("Failed to register service {} because of {:?}.", KS2_SERVICE_NAME, e);
81 });
82
Janis Danisevskisb1673db2021-02-08 18:11:57 -080083 let apc_service =
84 ApcManager::new_native_binder(confirmation_token_sender).unwrap_or_else(|e| {
85 panic!("Failed to create service {} because of {:?}.", APC_SERVICE_NAME, e);
86 });
Janis Danisevskis7a1cf382020-11-20 11:22:14 -080087 binder::add_service(APC_SERVICE_NAME, apc_service.as_binder()).unwrap_or_else(|e| {
88 panic!("Failed to register service {} because of {:?}.", APC_SERVICE_NAME, e);
89 });
90
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000091 let authorization_service = AuthorizationManager::new_native_binder().unwrap_or_else(|e| {
92 panic!("Failed to create service {} because of {:?}.", AUTHORIZATION_SERVICE_NAME, e);
93 });
94 binder::add_service(AUTHORIZATION_SERVICE_NAME, authorization_service.as_binder())
95 .unwrap_or_else(|e| {
96 panic!("Failed to register service {} because of {:?}.", AUTHORIZATION_SERVICE_NAME, e);
97 });
98
Janis Danisevskis34a0cf22021-03-08 09:19:03 -080099 let maintenance_service = Maintenance::new_native_binder().unwrap_or_else(|e| {
Hasini Gunasingheda895552021-01-27 19:34:37 +0000100 panic!("Failed to create service {} because of {:?}.", USER_MANAGER_SERVICE_NAME, e);
101 });
Janis Danisevskis34a0cf22021-03-08 09:19:03 -0800102 binder::add_service(USER_MANAGER_SERVICE_NAME, maintenance_service.as_binder()).unwrap_or_else(
Hasini Gunasingheda895552021-01-27 19:34:37 +0000103 |e| {
104 panic!("Failed to register service {} because of {:?}.", USER_MANAGER_SERVICE_NAME, e);
105 },
106 );
107
Max Biresb2e1d032021-02-08 21:35:05 -0800108 // Devices with KS2 and KM 1.0 may not have any IRemotelyProvisionedComponent HALs at all. Do
109 // not panic if new_native_binder returns failure because it could not find the TEE HAL.
110 if let Ok(remote_provisioning_service) = RemoteProvisioningService::new_native_binder() {
111 binder::add_service(
112 REMOTE_PROVISIONING_SERVICE_NAME,
113 remote_provisioning_service.as_binder(),
114 )
115 .unwrap_or_else(|e| {
116 panic!(
117 "Failed to register service {} because of {:?}.",
118 REMOTE_PROVISIONING_SERVICE_NAME, e
119 );
120 });
121 }
Janis Danisevskis77d72042021-01-20 15:36:30 -0800122
123 let vpnprofilestore = VpnProfileStore::new_native_binder(
Seth Moorea3e611a2021-05-11 10:07:45 -0700124 &keystore2::globals::DB_PATH.read().expect("Could not get DB_PATH."),
Janis Danisevskis77d72042021-01-20 15:36:30 -0800125 );
126 binder::add_service(VPNPROFILESTORE_SERVICE_NAME, vpnprofilestore.as_binder()).unwrap_or_else(
127 |e| {
128 panic!(
129 "Failed to register service {} because of {:?}.",
130 VPNPROFILESTORE_SERVICE_NAME, e
131 );
132 },
133 );
134
Seth Moore78c091f2021-04-09 21:38:30 +0000135 std::thread::spawn(|| {
136 match metrics::register_pull_metrics_callbacks() {
137 Err(e) => error!("register_pull_metrics_callbacks failed: {:?}.", e),
138 _ => info!("Pull metrics callbacks successfully registered."),
139 };
140 });
141
Janis Danisevskis1af91262020-08-10 14:58:08 -0700142 info!("Successfully registered Keystore 2.0 service.");
Janis Danisevskis1af91262020-08-10 14:58:08 -0700143
Janis Danisevskise242b032020-11-17 15:57:51 -0800144 info!("Joining thread pool now.");
Janis Danisevskis1af91262020-08-10 14:58:08 -0700145 binder::ProcessState::join_thread_pool();
146}