blob: c75cfc86a882643f05b1c4d6f1642d195f462c94 [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
17use binder::Interface;
Janis Danisevskis7a1cf382020-11-20 11:22:14 -080018use keystore2::apc::ApcManager;
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000019use keystore2::authorization::AuthorizationManager;
Hasini Gunasinghef04d07a2020-11-25 22:41:35 +000020use keystore2::background_task_handler::Message;
21use keystore2::globals::{BACKGROUND_TASK_HANDLER, ENFORCEMENTS};
Janis Danisevskis1af91262020-08-10 14:58:08 -070022use keystore2::service::KeystoreService;
23use log::{error, info};
24use std::panic;
Hasini Gunasinghef04d07a2020-11-25 22:41:35 +000025use std::sync::mpsc::channel;
Janis Danisevskis1af91262020-08-10 14:58:08 -070026
27static KS2_SERVICE_NAME: &str = "android.system.keystore2";
Janis Danisevskis7a1cf382020-11-20 11:22:14 -080028static APC_SERVICE_NAME: &str = "android.security.apc";
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000029static AUTHORIZATION_SERVICE_NAME: &str = "android.security.authorization";
Janis Danisevskis1af91262020-08-10 14:58:08 -070030
31/// Keystore 2.0 takes one argument which is a path indicating its designated working directory.
32fn main() {
33 // Initialize android logging.
34 android_logger::init_once(
35 android_logger::Config::default().with_tag("keystore2").with_min_level(log::Level::Debug),
36 );
37 // Redirect panic messages to logcat.
38 panic::set_hook(Box::new(|panic_info| {
39 error!("{}", panic_info);
40 }));
41
42 // Saying hi.
43 info!("Keystore2 is starting.");
44
45 let mut args = std::env::args();
46 args.next().expect("That's odd. How is there not even a first argument?");
Janis Danisevskisbf15d732020-12-08 10:35:26 -080047
48 // Keystore changes to the database directory on startup (typically /data/misc/keystore).
49 // For the ground truth check the service startup rule for init (typically in keystore2.rc).
Janis Danisevskis1af91262020-08-10 14:58:08 -070050 if let Some(dir) = args.next() {
51 if std::env::set_current_dir(dir.clone()).is_err() {
52 panic!("Failed to set working directory {}.", dir)
53 }
54 } else {
55 panic!("Must specify a working directory.");
56 }
57
Hasini Gunasinghef04d07a2020-11-25 22:41:35 +000058 // initialize the channel via which the enforcement module and background task handler module
59 // communicate, and hand over the sender and receiver ends to the respective objects.
60 let (sender, receiver) = channel::<Message>();
61 ENFORCEMENTS.set_sender_to_bth(sender);
62 BACKGROUND_TASK_HANDLER.start_bth(receiver).unwrap_or_else(|e| {
63 panic!("Failed to start background task handler because of {:?}.", e);
64 });
65
Janis Danisevskis8c6378e2021-01-01 09:30:37 -080066 info!("Starting thread pool now.");
67 binder::ProcessState::start_thread_pool();
68
Janis Danisevskis1af91262020-08-10 14:58:08 -070069 let ks_service = KeystoreService::new_native_binder().unwrap_or_else(|e| {
70 panic!("Failed to create service {} because of {:?}.", KS2_SERVICE_NAME, e);
71 });
72 binder::add_service(KS2_SERVICE_NAME, ks_service.as_binder()).unwrap_or_else(|e| {
73 panic!("Failed to register service {} because of {:?}.", KS2_SERVICE_NAME, e);
74 });
75
Janis Danisevskis7a1cf382020-11-20 11:22:14 -080076 let apc_service = ApcManager::new_native_binder().unwrap_or_else(|e| {
77 panic!("Failed to create service {} because of {:?}.", APC_SERVICE_NAME, e);
78 });
79 binder::add_service(APC_SERVICE_NAME, apc_service.as_binder()).unwrap_or_else(|e| {
80 panic!("Failed to register service {} because of {:?}.", APC_SERVICE_NAME, e);
81 });
82
Janis Danisevskis9f10a6a2021-01-18 16:45:21 +000083 let authorization_service = AuthorizationManager::new_native_binder().unwrap_or_else(|e| {
84 panic!("Failed to create service {} because of {:?}.", AUTHORIZATION_SERVICE_NAME, e);
85 });
86 binder::add_service(AUTHORIZATION_SERVICE_NAME, authorization_service.as_binder())
87 .unwrap_or_else(|e| {
88 panic!("Failed to register service {} because of {:?}.", AUTHORIZATION_SERVICE_NAME, e);
89 });
90
Janis Danisevskis1af91262020-08-10 14:58:08 -070091 info!("Successfully registered Keystore 2.0 service.");
Janis Danisevskis1af91262020-08-10 14:58:08 -070092
Janis Danisevskise242b032020-11-17 15:57:51 -080093 info!("Joining thread pool now.");
Janis Danisevskis1af91262020-08-10 14:58:08 -070094 binder::ProcessState::join_thread_pool();
95}