Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 1 | // Copyright 2021, 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 | |
| 15 | //! Main entry point for diced, the friendly neighborhood DICE service. |
| 16 | |
Janis Danisevskis | 41e6719 | 2021-11-05 09:00:19 -0700 | [diff] [blame] | 17 | use binder::get_interface; |
| 18 | use diced::{DiceMaintenance, DiceNode, DiceNodeImpl, ProxyNodeHal, ResidentNode}; |
Janis Danisevskis | 99652dc | 2021-10-20 15:59:33 -0700 | [diff] [blame] | 19 | use std::convert::TryInto; |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 20 | use std::panic; |
| 21 | use std::sync::Arc; |
| 22 | |
Janis Danisevskis | 99652dc | 2021-10-20 15:59:33 -0700 | [diff] [blame] | 23 | static DICE_NODE_SERVICE_NAME: &str = "android.security.dice.IDiceNode"; |
| 24 | static DICE_MAINTENANCE_SERVICE_NAME: &str = "android.security.dice.IDiceMaintenance"; |
Janis Danisevskis | 41e6719 | 2021-11-05 09:00:19 -0700 | [diff] [blame] | 25 | static DICE_HAL_SERVICE_NAME: &str = "android.hardware.security.dice.IDiceDevice/default"; |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 26 | |
| 27 | fn main() { |
| 28 | android_logger::init_once( |
| 29 | android_logger::Config::default().with_tag("diced").with_min_level(log::Level::Debug), |
| 30 | ); |
| 31 | // Redirect panic messages to logcat. |
| 32 | panic::set_hook(Box::new(|panic_info| { |
| 33 | log::error!("{}", panic_info); |
| 34 | })); |
| 35 | |
| 36 | // Saying hi. |
| 37 | log::info!("Diced, your friendly neighborhood DICE service, is starting."); |
| 38 | |
Janis Danisevskis | 41e6719 | 2021-11-05 09:00:19 -0700 | [diff] [blame] | 39 | let node_impl: Arc<dyn DiceNodeImpl + Send + Sync> = match get_interface(DICE_HAL_SERVICE_NAME) |
| 40 | { |
| 41 | Ok(dice_device) => { |
| 42 | Arc::new(ProxyNodeHal::new(dice_device).expect("Failed to construct a proxy node.")) |
| 43 | } |
| 44 | Err(e) => { |
| 45 | log::warn!("Failed to connect to DICE HAL: {:?}", e); |
| 46 | log::warn!("Using sample dice artifacts."); |
| 47 | let (cdi_attest, cdi_seal, bcc) = diced_sample_inputs::make_sample_bcc_and_cdis() |
| 48 | .expect("Failed to create sample dice artifacts."); |
| 49 | Arc::new( |
| 50 | ResidentNode::new( |
| 51 | cdi_attest[..] |
| 52 | .try_into() |
| 53 | .expect("Failed to convert cdi_attest into array ref."), |
| 54 | cdi_seal[..].try_into().expect("Failed to convert cdi_seal into array ref."), |
| 55 | bcc, |
| 56 | ) |
| 57 | .expect("Failed to construct a resident node."), |
| 58 | ) |
| 59 | } |
| 60 | }; |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 61 | |
Janis Danisevskis | 99652dc | 2021-10-20 15:59:33 -0700 | [diff] [blame] | 62 | let node = DiceNode::new_as_binder(node_impl.clone()) |
| 63 | .expect("Failed to create IDiceNode service instance."); |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 64 | |
Janis Danisevskis | 99652dc | 2021-10-20 15:59:33 -0700 | [diff] [blame] | 65 | let maintenance = DiceMaintenance::new_as_binder(node_impl) |
| 66 | .expect("Failed to create IDiceMaintenance service instance."); |
| 67 | |
| 68 | binder::add_service(DICE_NODE_SERVICE_NAME, node.as_binder()) |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 69 | .expect("Failed to register IDiceNode Service"); |
| 70 | |
Janis Danisevskis | 99652dc | 2021-10-20 15:59:33 -0700 | [diff] [blame] | 71 | binder::add_service(DICE_MAINTENANCE_SERVICE_NAME, maintenance.as_binder()) |
| 72 | .expect("Failed to register IDiceMaintenance Service"); |
| 73 | |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 74 | log::info!("Joining thread pool now."); |
| 75 | binder::ProcessState::join_thread_pool(); |
| 76 | } |