blob: 0197f2c71ce32c0ff5390344fecf8c0c705b80fe [file] [log] [blame]
Janis Danisevskis86124732021-11-09 23:00:26 -08001// 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 the android.hardware.security.dice service.
16
17use anyhow::Result;
18use diced::{
19 dice,
20 hal_node::{DiceArtifacts, DiceDevice, ResidentHal, UpdatableDiceArtifacts},
21};
22use diced_sample_inputs::make_sample_bcc_and_cdis;
23use serde::{Deserialize, Serialize};
Janis Danisevskis86124732021-11-09 23:00:26 -080024use std::panic;
25use std::sync::Arc;
26
27static DICE_HAL_SERVICE_NAME: &str = "android.hardware.security.dice.IDiceDevice/default";
28
29#[derive(Debug, Serialize, Deserialize, Clone)]
30struct InsecureSerializableArtifacts {
31 cdi_attest: [u8; dice::CDI_SIZE],
32 cdi_seal: [u8; dice::CDI_SIZE],
33 bcc: Vec<u8>,
34}
35
36impl DiceArtifacts for InsecureSerializableArtifacts {
37 fn cdi_attest(&self) -> &[u8; dice::CDI_SIZE] {
38 &self.cdi_attest
39 }
40 fn cdi_seal(&self) -> &[u8; dice::CDI_SIZE] {
41 &self.cdi_seal
42 }
43 fn bcc(&self) -> Vec<u8> {
44 self.bcc.clone()
45 }
46}
47
48impl UpdatableDiceArtifacts for InsecureSerializableArtifacts {
49 fn with_artifacts<F, T>(&self, f: F) -> Result<T>
50 where
51 F: FnOnce(&dyn DiceArtifacts) -> Result<T>,
52 {
53 f(self)
54 }
55 fn update(self, new_artifacts: &impl DiceArtifacts) -> Result<Self> {
56 Ok(Self {
57 cdi_attest: *new_artifacts.cdi_attest(),
58 cdi_seal: *new_artifacts.cdi_seal(),
59 bcc: new_artifacts.bcc(),
60 })
61 }
62}
63
64fn main() {
65 android_logger::init_once(
66 android_logger::Config::default()
67 .with_tag("android.hardware.security.dice")
68 .with_min_level(log::Level::Debug),
69 );
70 // Redirect panic messages to logcat.
71 panic::set_hook(Box::new(|panic_info| {
72 log::error!("{}", panic_info);
73 }));
74
75 // Saying hi.
76 log::info!("android.hardware.security.dice is starting.");
77
Alice Wangcfb4e922023-02-08 09:26:46 +000078 let dice_artifacts =
Janis Danisevskis86124732021-11-09 23:00:26 -080079 make_sample_bcc_and_cdis().expect("Failed to construct sample dice chain.");
80
81 let hal_impl = Arc::new(
82 unsafe {
83 // Safety: ResidentHal cannot be used in multi threaded processes.
84 // This service does not start a thread pool. The main thread is the only thread
85 // joining the thread pool, thereby keeping the process single threaded.
86 ResidentHal::new(InsecureSerializableArtifacts {
Alice Wangcfb4e922023-02-08 09:26:46 +000087 cdi_attest: dice_artifacts.cdi_values.cdi_attest,
88 cdi_seal: dice_artifacts.cdi_values.cdi_seal,
89 bcc: dice_artifacts.bcc[..].to_vec(),
Janis Danisevskis86124732021-11-09 23:00:26 -080090 })
91 }
92 .expect("Failed to create ResidentHal implementation."),
93 );
94
95 let hal = DiceDevice::new_as_binder(hal_impl).expect("Failed to construct hal service.");
96
97 binder::add_service(DICE_HAL_SERVICE_NAME, hal.as_binder())
98 .expect("Failed to register IDiceDevice Service");
99
100 log::info!("Joining thread pool now.");
101 binder::ProcessState::join_thread_pool();
102}