David Drysdale | 7fd838c | 2023-10-05 13:07:28 +0100 | [diff] [blame] | 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 | |
David Drysdale | 6c09af2 | 2023-11-06 09:57:10 +0000 | [diff] [blame] | 25 | use authgraph_hal::service; |
| 26 | use authgraph_nonsecure::LocalTa; |
David Drysdale | 7fd838c | 2023-10-05 13:07:28 +0100 | [diff] [blame] | 27 | use log::{error, info}; |
David Drysdale | 6c09af2 | 2023-11-06 09:57:10 +0000 | [diff] [blame] | 28 | use std::sync::{Arc, Mutex}; |
David Drysdale | 7fd838c | 2023-10-05 13:07:28 +0100 | [diff] [blame] | 29 | |
| 30 | static SERVICE_NAME: &str = "android.hardware.security.authgraph.IAuthGraphKeyExchange"; |
| 31 | static SERVICE_INSTANCE: &str = "nonsecure"; |
| 32 | |
| 33 | /// Local error type for failures in the HAL service. |
| 34 | #[derive(Debug, Clone)] |
| 35 | struct HalServiceError(String); |
| 36 | |
| 37 | impl From<String> for HalServiceError { |
| 38 | fn from(s: String) -> Self { |
| 39 | Self(s) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | fn main() { |
| 44 | if let Err(e) = inner_main() { |
| 45 | panic!("HAL service failed: {:?}", e); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | fn 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 Gunasinghe | 5df6ed5 | 2023-11-13 09:18:25 +0000 | [diff] [blame] | 68 | let local_ta = |
| 69 | LocalTa::new().map_err(|e| format!("Failed to create the TA because: {e:?}"))?; |
David Drysdale | 6c09af2 | 2023-11-06 09:57:10 +0000 | [diff] [blame] | 70 | let service = service::AuthGraphService::new_as_binder(Arc::new(Mutex::new(local_ta))); |
David Drysdale | 7fd838c | 2023-10-05 13:07:28 +0100 | [diff] [blame] | 71 | 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 | } |