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 //! Default implementation of the AuthGraph key exchange HAL.
18 //!
19 //! This implementation of the HAL is only intended to allow testing and policy compliance.  A real
20 //! implementation of the AuthGraph HAL would be implemented in a secure environment, and would not
21 //! be independently registered with service manager (a secure component that uses AuthGraph would
22 //! expose an entrypoint that allowed retrieval of the specific IAuthGraphKeyExchange instance that
23 //! is correlated with the component).
24 
25 use authgraph_hal::service;
26 use authgraph_nonsecure::LocalTa;
27 use log::{error, info};
28 
29 static SERVICE_NAME: &str = "android.hardware.security.authgraph.IAuthGraphKeyExchange";
30 static SERVICE_INSTANCE: &str = "nonsecure";
31 
32 /// Local error type for failures in the HAL service.
33 #[derive(Debug, Clone)]
34 struct HalServiceError(String);
35 
36 impl From<String> for HalServiceError {
from(s: String) -> Self37     fn from(s: String) -> Self {
38         Self(s)
39     }
40 }
41 
main()42 fn main() {
43     if let Err(HalServiceError(e)) = inner_main() {
44         panic!("HAL service failed: {:?}", e);
45     }
46 }
47 
inner_main() -> Result<(), HalServiceError>48 fn inner_main() -> Result<(), HalServiceError> {
49     // Initialize Android logging.
50     android_logger::init_once(
51         android_logger::Config::default()
52             .with_tag("authgraph-hal-nonsecure")
53             .with_max_level(log::LevelFilter::Info)
54             .with_log_buffer(android_logger::LogId::System),
55     );
56     // Redirect panic messages to logcat.
57     std::panic::set_hook(Box::new(|panic_info| {
58         error!("{}", panic_info);
59     }));
60 
61     info!("Insecure AuthGraph key exchange HAL service is starting.");
62 
63     info!("Starting thread pool now.");
64     binder::ProcessState::start_thread_pool();
65 
66     // Register the service
67     let local_ta = LocalTa::new().map_err(|e| format!("Failed to create the TA because: {e:?}"))?;
68     let service = service::AuthGraphService::new_as_binder(local_ta);
69     let service_name = format!("{}/{}", SERVICE_NAME, SERVICE_INSTANCE);
70     binder::add_service(&service_name, service.as_binder()).map_err(|e| {
71         format!(
72             "Failed to register service {} because of {:?}.",
73             service_name, e
74         )
75     })?;
76 
77     info!("Successfully registered AuthGraph HAL services.");
78     binder::ProcessState::join_thread_pool();
79     info!("AuthGraph HAL service is terminating."); // should not reach here
80     Ok(())
81 }
82