blob: 4363e91ec3160bda9609131c0c4569e8e67e8cad [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
Alice Wange5850652023-02-15 16:10:36 +000017use anyhow::{anyhow, Result};
Janis Danisevskis86124732021-11-09 23:00:26 -080018use 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 }
Alice Wange5850652023-02-15 16:10:36 +000043 fn bcc(&self) -> Option<&[u8]> {
44 Some(&self.bcc)
Janis Danisevskis86124732021-11-09 23:00:26 -080045 }
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(),
Alice Wange5850652023-02-15 16:10:36 +000059 bcc: new_artifacts
60 .bcc()
61 .ok_or_else(|| anyhow!("bcc is none"))?
62 .to_vec(),
Janis Danisevskis86124732021-11-09 23:00:26 -080063 })
64 }
65}
66
67fn main() {
68 android_logger::init_once(
69 android_logger::Config::default()
70 .with_tag("android.hardware.security.dice")
71 .with_min_level(log::Level::Debug),
72 );
73 // Redirect panic messages to logcat.
74 panic::set_hook(Box::new(|panic_info| {
75 log::error!("{}", panic_info);
76 }));
77
78 // Saying hi.
79 log::info!("android.hardware.security.dice is starting.");
80
Alice Wangcfb4e922023-02-08 09:26:46 +000081 let dice_artifacts =
Janis Danisevskis86124732021-11-09 23:00:26 -080082 make_sample_bcc_and_cdis().expect("Failed to construct sample dice chain.");
Alice Wange5850652023-02-15 16:10:36 +000083 let mut cdi_attest = [0u8; dice::CDI_SIZE];
84 cdi_attest.copy_from_slice(dice_artifacts.cdi_attest());
85 let mut cdi_seal = [0u8; dice::CDI_SIZE];
86 cdi_seal.copy_from_slice(dice_artifacts.cdi_seal());
Janis Danisevskis86124732021-11-09 23:00:26 -080087 let hal_impl = Arc::new(
88 unsafe {
89 // Safety: ResidentHal cannot be used in multi threaded processes.
90 // This service does not start a thread pool. The main thread is the only thread
91 // joining the thread pool, thereby keeping the process single threaded.
92 ResidentHal::new(InsecureSerializableArtifacts {
Alice Wange5850652023-02-15 16:10:36 +000093 cdi_attest,
94 cdi_seal,
95 bcc: dice_artifacts.bcc().expect("bcc is none").to_vec(),
Janis Danisevskis86124732021-11-09 23:00:26 -080096 })
97 }
98 .expect("Failed to create ResidentHal implementation."),
99 );
100
101 let hal = DiceDevice::new_as_binder(hal_impl).expect("Failed to construct hal service.");
102
103 binder::add_service(DICE_HAL_SERVICE_NAME, hal.as_binder())
104 .expect("Failed to register IDiceDevice Service");
105
106 log::info!("Joining thread pool now.");
107 binder::ProcessState::join_thread_pool();
108}