Janis Danisevskis | 8612473 | 2021-11-09 23:00:26 -0800 | [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 the android.hardware.security.dice service. |
| 16 | |
| 17 | use anyhow::Result; |
| 18 | use diced::{ |
| 19 | dice, |
| 20 | hal_node::{DiceArtifacts, DiceDevice, ResidentHal, UpdatableDiceArtifacts}, |
| 21 | }; |
| 22 | use diced_sample_inputs::make_sample_bcc_and_cdis; |
| 23 | use serde::{Deserialize, Serialize}; |
Janis Danisevskis | 8612473 | 2021-11-09 23:00:26 -0800 | [diff] [blame] | 24 | use std::panic; |
| 25 | use std::sync::Arc; |
| 26 | |
| 27 | static DICE_HAL_SERVICE_NAME: &str = "android.hardware.security.dice.IDiceDevice/default"; |
| 28 | |
| 29 | #[derive(Debug, Serialize, Deserialize, Clone)] |
| 30 | struct InsecureSerializableArtifacts { |
| 31 | cdi_attest: [u8; dice::CDI_SIZE], |
| 32 | cdi_seal: [u8; dice::CDI_SIZE], |
| 33 | bcc: Vec<u8>, |
| 34 | } |
| 35 | |
| 36 | impl 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 | |
| 48 | impl 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 | |
| 64 | fn 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 Wang | cfb4e92 | 2023-02-08 09:26:46 +0000 | [diff] [blame] | 78 | let dice_artifacts = |
Janis Danisevskis | 8612473 | 2021-11-09 23:00:26 -0800 | [diff] [blame] | 79 | 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 Wang | cfb4e92 | 2023-02-08 09:26:46 +0000 | [diff] [blame] | 87 | 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 Danisevskis | 8612473 | 2021-11-09 23:00:26 -0800 | [diff] [blame] | 90 | }) |
| 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 | } |