Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 1 | // Copyright 2023, 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 | //! This module implements a retry version for multiple DICE functions that |
| 16 | //! require preallocated output buffer. As the retry functions require |
| 17 | //! memory allocation on heap, currently we only expose these functions in |
| 18 | //! std environment. |
| 19 | |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame] | 20 | use crate::bcc::{bcc_format_config_descriptor, bcc_main_flow}; |
Alice Wang | 4d3059a | 2023-02-15 10:24:33 +0000 | [diff] [blame] | 21 | use crate::dice::{ |
| 22 | dice_main_flow, Cdi, CdiValues, DiceArtifacts, InputValues, CDI_SIZE, PRIVATE_KEY_SEED_SIZE, |
| 23 | }; |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 24 | use crate::error::{DiceError, Result}; |
Alice Wang | 4d61177 | 2023-02-13 09:45:21 +0000 | [diff] [blame] | 25 | use crate::ops::generate_certificate; |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 26 | use std::ffi::CStr; |
| 27 | |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame] | 28 | /// Artifacts stores a set of dice artifacts comprising CDI_ATTEST, CDI_SEAL, |
| 29 | /// and the BCC formatted attestation certificate chain. |
| 30 | /// As we align with the DICE standards today, this is the certificate chain |
| 31 | /// is also called DICE certificate chain. |
Alice Wang | acee4fb | 2023-02-15 09:42:07 +0000 | [diff] [blame] | 32 | #[derive(Debug)] |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame] | 33 | pub struct OwnedDiceArtifacts { |
| 34 | /// CDI Values. |
Alice Wang | 4d3059a | 2023-02-15 10:24:33 +0000 | [diff] [blame] | 35 | cdi_values: CdiValues, |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame] | 36 | /// Boot Certificate Chain. |
Alice Wang | 4d3059a | 2023-02-15 10:24:33 +0000 | [diff] [blame] | 37 | bcc: Vec<u8>, |
| 38 | } |
| 39 | |
| 40 | impl DiceArtifacts for OwnedDiceArtifacts { |
| 41 | fn cdi_attest(&self) -> &[u8; CDI_SIZE] { |
| 42 | &self.cdi_values.cdi_attest |
| 43 | } |
| 44 | |
| 45 | fn cdi_seal(&self) -> &[u8; CDI_SIZE] { |
| 46 | &self.cdi_values.cdi_seal |
| 47 | } |
| 48 | |
| 49 | fn bcc(&self) -> Option<&[u8]> { |
| 50 | Some(&self.bcc) |
| 51 | } |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 54 | /// Retries the given function with bigger measured buffer size. |
| 55 | fn retry_with_measured_buffer<F>(mut f: F) -> Result<Vec<u8>> |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 56 | where |
| 57 | F: FnMut(&mut Vec<u8>) -> Result<usize>, |
| 58 | { |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 59 | let mut buffer = Vec::new(); |
| 60 | match f(&mut buffer) { |
| 61 | Err(DiceError::BufferTooSmall(actual_size)) => { |
| 62 | buffer.resize(actual_size, 0); |
| 63 | f(&mut buffer)?; |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 64 | } |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 65 | Err(e) => return Err(e), |
| 66 | Ok(_) => {} |
| 67 | }; |
| 68 | Ok(buffer) |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /// Formats a configuration descriptor following the BCC's specification. |
| 72 | pub fn retry_bcc_format_config_descriptor( |
| 73 | name: Option<&CStr>, |
| 74 | version: Option<u64>, |
| 75 | resettable: bool, |
| 76 | ) -> Result<Vec<u8>> { |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 77 | retry_with_measured_buffer(|buffer| { |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 78 | bcc_format_config_descriptor(name, version, resettable, buffer) |
| 79 | }) |
| 80 | } |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame] | 81 | |
| 82 | /// Executes the main BCC flow. |
| 83 | /// |
| 84 | /// Given a full set of input values along with the current BCC and CDI values, |
| 85 | /// computes the next CDI values and matching updated BCC. |
| 86 | pub fn retry_bcc_main_flow( |
| 87 | current_cdi_attest: &Cdi, |
| 88 | current_cdi_seal: &Cdi, |
| 89 | bcc: &[u8], |
| 90 | input_values: &InputValues, |
| 91 | ) -> Result<OwnedDiceArtifacts> { |
| 92 | let mut next_cdi_values = CdiValues::default(); |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 93 | let next_bcc = retry_with_measured_buffer(|next_bcc| { |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame] | 94 | bcc_main_flow( |
| 95 | current_cdi_attest, |
| 96 | current_cdi_seal, |
| 97 | bcc, |
| 98 | input_values, |
| 99 | &mut next_cdi_values, |
| 100 | next_bcc, |
| 101 | ) |
| 102 | })?; |
| 103 | Ok(OwnedDiceArtifacts { cdi_values: next_cdi_values, bcc: next_bcc }) |
| 104 | } |
Alice Wang | 44f48b2 | 2023-02-09 09:51:22 +0000 | [diff] [blame] | 105 | |
| 106 | /// Executes the main DICE flow. |
| 107 | /// |
| 108 | /// Given a full set of input values and the current CDI values, computes the |
| 109 | /// next CDI values and a matching certificate. |
| 110 | pub fn retry_dice_main_flow( |
| 111 | current_cdi_attest: &Cdi, |
| 112 | current_cdi_seal: &Cdi, |
| 113 | input_values: &InputValues, |
| 114 | ) -> Result<(CdiValues, Vec<u8>)> { |
| 115 | let mut next_cdi_values = CdiValues::default(); |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 116 | let next_cdi_certificate = retry_with_measured_buffer(|next_cdi_certificate| { |
Alice Wang | 44f48b2 | 2023-02-09 09:51:22 +0000 | [diff] [blame] | 117 | dice_main_flow( |
| 118 | current_cdi_attest, |
| 119 | current_cdi_seal, |
| 120 | input_values, |
| 121 | next_cdi_certificate, |
| 122 | &mut next_cdi_values, |
| 123 | ) |
| 124 | })?; |
| 125 | Ok((next_cdi_values, next_cdi_certificate)) |
| 126 | } |
Alice Wang | 4d61177 | 2023-02-13 09:45:21 +0000 | [diff] [blame] | 127 | |
| 128 | /// Generates an X.509 certificate from the given `subject_private_key_seed` and |
| 129 | /// `input_values`, and signed by `authority_private_key_seed`. |
| 130 | /// The subject private key seed is supplied here so the implementation can choose |
| 131 | /// between asymmetric mechanisms, for example ECDSA vs Ed25519. |
| 132 | /// Returns the generated certificate. |
| 133 | pub fn retry_generate_certificate( |
| 134 | subject_private_key_seed: &[u8; PRIVATE_KEY_SEED_SIZE], |
| 135 | authority_private_key_seed: &[u8; PRIVATE_KEY_SEED_SIZE], |
| 136 | input_values: &InputValues, |
| 137 | ) -> Result<Vec<u8>> { |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 138 | retry_with_measured_buffer(|certificate| { |
Alice Wang | 4d61177 | 2023-02-13 09:45:21 +0000 | [diff] [blame] | 139 | generate_certificate( |
| 140 | subject_private_key_seed, |
| 141 | authority_private_key_seed, |
| 142 | input_values, |
| 143 | certificate, |
| 144 | ) |
| 145 | }) |
| 146 | } |