blob: c2cf02c8f3060e729f6c5014f1817fae8f9e95fd [file] [log] [blame]
Janis Danisevskisc51dff82021-10-20 09:51:16 -07001// 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 Danisevskis41e67192021-11-05 09:00:19 -070017use binder::get_interface;
18use diced::{DiceMaintenance, DiceNode, DiceNodeImpl, ProxyNodeHal, ResidentNode};
Janis Danisevskis99652dc2021-10-20 15:59:33 -070019use std::convert::TryInto;
Janis Danisevskisc51dff82021-10-20 09:51:16 -070020use std::panic;
21use std::sync::Arc;
22
Janis Danisevskis99652dc2021-10-20 15:59:33 -070023static DICE_NODE_SERVICE_NAME: &str = "android.security.dice.IDiceNode";
24static DICE_MAINTENANCE_SERVICE_NAME: &str = "android.security.dice.IDiceMaintenance";
Janis Danisevskis41e67192021-11-05 09:00:19 -070025static DICE_HAL_SERVICE_NAME: &str = "android.hardware.security.dice.IDiceDevice/default";
Janis Danisevskisc51dff82021-10-20 09:51:16 -070026
27fn 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 Danisevskis41e67192021-11-05 09:00:19 -070039 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 Danisevskisc51dff82021-10-20 09:51:16 -070061
Janis Danisevskis99652dc2021-10-20 15:59:33 -070062 let node = DiceNode::new_as_binder(node_impl.clone())
63 .expect("Failed to create IDiceNode service instance.");
Janis Danisevskisc51dff82021-10-20 09:51:16 -070064
Janis Danisevskis99652dc2021-10-20 15:59:33 -070065 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 Danisevskisc51dff82021-10-20 09:51:16 -070069 .expect("Failed to register IDiceNode Service");
70
Janis Danisevskis99652dc2021-10-20 15:59:33 -070071 binder::add_service(DICE_MAINTENANCE_SERVICE_NAME, maintenance.as_binder())
72 .expect("Failed to register IDiceMaintenance Service");
73
Janis Danisevskisc51dff82021-10-20 09:51:16 -070074 log::info!("Joining thread pool now.");
75 binder::ProcessState::join_thread_pool();
76}