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