blob: 34016543c7764d8facf5630ca3da9ed94060a83f [file] [log] [blame]
Andrew Scullf8195942022-01-13 17:37:52 +00001// Copyright 2022, 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 microdroid IDiceDevice HAL implementation.
16
17use anyhow::Result;
18use diced::{
19 dice,
20 hal_node::{DiceArtifacts, DiceDevice, ResidentHal, UpdatableDiceArtifacts},
21};
22use serde::{Deserialize, Serialize};
23use std::panic;
24use std::sync::Arc;
25
26const DICE_HAL_SERVICE_NAME: &str = "android.hardware.security.dice.IDiceDevice/default";
27
28/// Artifacts that are kept in the process address space after the artifacts
29/// from the driver have been consumed.
30#[derive(Clone, Serialize, Deserialize)]
31struct RawArtifacts {
32 cdi_attest: [u8; dice::CDI_SIZE],
33 cdi_seal: [u8; dice::CDI_SIZE],
34 bcc: Vec<u8>,
35}
36
37impl DiceArtifacts for RawArtifacts {
38 fn cdi_attest(&self) -> &[u8; dice::CDI_SIZE] {
39 &self.cdi_attest
40 }
41 fn cdi_seal(&self) -> &[u8; dice::CDI_SIZE] {
42 &self.cdi_seal
43 }
44 fn bcc(&self) -> Vec<u8> {
45 // The BCC only contains public information so it's fine to copy.
46 self.bcc.clone()
47 }
48}
49
50#[derive(Clone, Serialize, Deserialize)]
51enum DriverArtifactManager {
52 Updated(RawArtifacts),
53}
54
55impl DriverArtifactManager {
56 fn new() -> Self {
57 // TODO(214231981): replace with true values passed by bootloader
58 let (cdi_attest, cdi_seal, bcc) = diced_sample_inputs::make_sample_bcc_and_cdis()
59 .expect("Failed to create sample dice artifacts.");
60 Self::Updated(RawArtifacts {
61 cdi_attest: cdi_attest[..].try_into().unwrap(),
62 cdi_seal: cdi_seal[..].try_into().unwrap(),
63 bcc,
64 })
65 }
66}
67
68impl UpdatableDiceArtifacts for DriverArtifactManager {
69 fn with_artifacts<F, T>(&self, f: F) -> Result<T>
70 where
71 F: FnOnce(&dyn DiceArtifacts) -> Result<T>,
72 {
73 match self {
74 Self::Updated(raw_artifacts) => f(raw_artifacts),
75 }
76 }
77 fn update(self, new_artifacts: &impl DiceArtifacts) -> Result<Self> {
78 Ok(Self::Updated(RawArtifacts {
79 cdi_attest: *new_artifacts.cdi_attest(),
80 cdi_seal: *new_artifacts.cdi_seal(),
81 bcc: new_artifacts.bcc(),
82 }))
83 }
84}
85
86fn main() {
87 android_logger::init_once(
88 android_logger::Config::default()
89 .with_tag("android.hardware.security.dice")
90 .with_min_level(log::Level::Debug),
91 );
92 // Redirect panic messages to logcat.
93 panic::set_hook(Box::new(|panic_info| {
94 log::error!("{}", panic_info);
95 }));
96
97 // Saying hi.
98 log::info!("android.hardware.security.dice is starting.");
99
100 let hal_impl = Arc::new(
101 unsafe {
102 // Safety: ResidentHal cannot be used in multi threaded processes.
103 // This service does not start a thread pool. The main thread is the only thread
104 // joining the thread pool, thereby keeping the process single threaded.
105 ResidentHal::new(DriverArtifactManager::new())
106 }
107 .expect("Failed to create ResidentHal implementation."),
108 );
109
110 let hal = DiceDevice::new_as_binder(hal_impl).expect("Failed to construct hal service.");
111
112 binder::add_service(DICE_HAL_SERVICE_NAME, hal.as_binder())
113 .expect("Failed to register IDiceDevice Service");
114
115 log::info!("Joining thread pool now.");
116 binder::ProcessState::join_thread_pool();
117}