1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //! Non-secure implementation of the Secretkeeper HAL.
18 
19 use log::{error, info, LevelFilter};
20 use secretkeeper_hal::SecretkeeperService;
21 use secretkeeper_nonsecure::{AuthGraphChannel, SecretkeeperChannel, LocalTa};
22 use std::sync::{Arc, Mutex};
23 use android_hardware_security_secretkeeper::aidl::android::hardware::security::secretkeeper::ISecretkeeper::{
24     BpSecretkeeper, ISecretkeeper,
25 };
26 
main()27 fn main() {
28     // Initialize Android logging.
29     android_logger::init_once(
30         android_logger::Config::default()
31             .with_tag("NonSecureSecretkeeper")
32             .with_max_level(LevelFilter::Info)
33             .with_log_buffer(android_logger::LogId::System),
34     );
35     // Redirect panic messages to logcat.
36     std::panic::set_hook(Box::new(|panic_info| {
37         error!("{}", panic_info);
38     }));
39 
40     let ta = Arc::new(Mutex::new(LocalTa::new()));
41     let ag_channel = AuthGraphChannel(ta.clone());
42     let sk_channel = SecretkeeperChannel(ta.clone());
43 
44     let service = SecretkeeperService::new_as_binder(sk_channel, ag_channel);
45     let service_name = format!(
46         "{}/nonsecure",
47         <BpSecretkeeper as ISecretkeeper>::get_descriptor()
48     );
49     binder::add_service(&service_name, service.as_binder()).unwrap_or_else(|e| {
50         panic!("Failed to register service {service_name} because of {e:?}.",);
51     });
52     info!("Registered Binder service, joining threadpool.");
53     binder::ProcessState::join_thread_pool();
54 }
55