blob: 81f2dd611583dc527d344a78606e7f960289f1aa [file] [log] [blame]
David Drysdale7fd838c2023-10-05 13:07:28 +01001/*
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
David Drysdale6c09af22023-11-06 09:57:10 +000025use authgraph_hal::service;
26use authgraph_nonsecure::LocalTa;
David Drysdale7fd838c2023-10-05 13:07:28 +010027use log::{error, info};
David Drysdale6c09af22023-11-06 09:57:10 +000028use std::sync::{Arc, Mutex};
David Drysdale7fd838c2023-10-05 13:07:28 +010029
30static SERVICE_NAME: &str = "android.hardware.security.authgraph.IAuthGraphKeyExchange";
31static SERVICE_INSTANCE: &str = "nonsecure";
32
33/// Local error type for failures in the HAL service.
34#[derive(Debug, Clone)]
35struct HalServiceError(String);
36
37impl From<String> for HalServiceError {
38 fn from(s: String) -> Self {
39 Self(s)
40 }
41}
42
43fn main() {
44 if let Err(e) = inner_main() {
45 panic!("HAL service failed: {:?}", e);
46 }
47}
48
49fn inner_main() -> Result<(), HalServiceError> {
50 // Initialize Android logging.
51 android_logger::init_once(
52 android_logger::Config::default()
53 .with_tag("authgraph-hal-nonsecure")
54 .with_min_level(log::Level::Info)
55 .with_log_id(android_logger::LogId::System),
56 );
57 // Redirect panic messages to logcat.
58 std::panic::set_hook(Box::new(|panic_info| {
59 error!("{}", panic_info);
60 }));
61
62 info!("Insecure AuthGraph key exchange HAL service is starting.");
63
64 info!("Starting thread pool now.");
65 binder::ProcessState::start_thread_pool();
66
67 // Register the service
Hasini Gunasinghe5df6ed52023-11-13 09:18:25 +000068 let local_ta =
69 LocalTa::new().map_err(|e| format!("Failed to create the TA because: {e:?}"))?;
David Drysdale6c09af22023-11-06 09:57:10 +000070 let service = service::AuthGraphService::new_as_binder(Arc::new(Mutex::new(local_ta)));
David Drysdale7fd838c2023-10-05 13:07:28 +010071 let service_name = format!("{}/{}", SERVICE_NAME, SERVICE_INSTANCE);
72 binder::add_service(&service_name, service.as_binder()).map_err(|e| {
73 format!(
74 "Failed to register service {} because of {:?}.",
75 service_name, e
76 )
77 })?;
78
79 info!("Successfully registered AuthGraph HAL services.");
80 binder::ProcessState::join_thread_pool();
81 info!("AuthGraph HAL service is terminating."); // should not reach here
82 Ok(())
83}