blob: 84ca5f5d2af69f396ff9ceef39f56db44240b328 [file] [log] [blame]
Alice Wang9c40eca2023-02-03 13:10:24 +00001// 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 Stokes82b12392023-08-22 14:39:29 +010020use crate::bcc::{bcc_format_config_descriptor, bcc_main_flow, DiceConfigValues};
Alice Wang4d3059a2023-02-15 10:24:33 +000021use crate::dice::{
22 dice_main_flow, Cdi, CdiValues, DiceArtifacts, InputValues, CDI_SIZE, PRIVATE_KEY_SEED_SIZE,
23};
Alice Wang9c40eca2023-02-03 13:10:24 +000024use crate::error::{DiceError, Result};
Alice Wang4d611772023-02-13 09:45:21 +000025use crate::ops::generate_certificate;
Alice Wang9c40eca2023-02-03 13:10:24 +000026
Alice Wangf4bd1c62023-02-08 08:38:44 +000027/// Artifacts stores a set of dice artifacts comprising CDI_ATTEST, CDI_SEAL,
28/// and the BCC formatted attestation certificate chain.
29/// As we align with the DICE standards today, this is the certificate chain
30/// is also called DICE certificate chain.
Alice Wangacee4fb2023-02-15 09:42:07 +000031#[derive(Debug)]
Alice Wangf4bd1c62023-02-08 08:38:44 +000032pub struct OwnedDiceArtifacts {
33 /// CDI Values.
Alice Wang4d3059a2023-02-15 10:24:33 +000034 cdi_values: CdiValues,
Alice Wangf4bd1c62023-02-08 08:38:44 +000035 /// Boot Certificate Chain.
Alice Wang4d3059a2023-02-15 10:24:33 +000036 bcc: Vec<u8>,
37}
38
39impl DiceArtifacts for OwnedDiceArtifacts {
40 fn cdi_attest(&self) -> &[u8; CDI_SIZE] {
41 &self.cdi_values.cdi_attest
42 }
43
44 fn cdi_seal(&self) -> &[u8; CDI_SIZE] {
45 &self.cdi_values.cdi_seal
46 }
47
48 fn bcc(&self) -> Option<&[u8]> {
49 Some(&self.bcc)
50 }
Alice Wangf4bd1c62023-02-08 08:38:44 +000051}
52
Alice Wangef999242023-05-22 11:14:59 +000053/// Retries the given function with bigger measured buffer size.
54fn retry_with_measured_buffer<F>(mut f: F) -> Result<Vec<u8>>
Alice Wang9c40eca2023-02-03 13:10:24 +000055where
56 F: FnMut(&mut Vec<u8>) -> Result<usize>,
57{
Alice Wangef999242023-05-22 11:14:59 +000058 let mut buffer = Vec::new();
59 match f(&mut buffer) {
60 Err(DiceError::BufferTooSmall(actual_size)) => {
61 buffer.resize(actual_size, 0);
62 f(&mut buffer)?;
Alice Wang9c40eca2023-02-03 13:10:24 +000063 }
Alice Wangef999242023-05-22 11:14:59 +000064 Err(e) => return Err(e),
65 Ok(_) => {}
66 };
67 Ok(buffer)
Alice Wang9c40eca2023-02-03 13:10:24 +000068}
69
70/// Formats a configuration descriptor following the BCC's specification.
Alan Stokes82b12392023-08-22 14:39:29 +010071pub fn retry_bcc_format_config_descriptor(values: &DiceConfigValues) -> Result<Vec<u8>> {
72 retry_with_measured_buffer(|buffer| bcc_format_config_descriptor(values, buffer))
Alice Wang9c40eca2023-02-03 13:10:24 +000073}
Alice Wangf4bd1c62023-02-08 08:38:44 +000074
75/// Executes the main BCC flow.
76///
77/// Given a full set of input values along with the current BCC and CDI values,
78/// computes the next CDI values and matching updated BCC.
79pub fn retry_bcc_main_flow(
80 current_cdi_attest: &Cdi,
81 current_cdi_seal: &Cdi,
82 bcc: &[u8],
83 input_values: &InputValues,
84) -> Result<OwnedDiceArtifacts> {
85 let mut next_cdi_values = CdiValues::default();
Alice Wangef999242023-05-22 11:14:59 +000086 let next_bcc = retry_with_measured_buffer(|next_bcc| {
Alice Wangf4bd1c62023-02-08 08:38:44 +000087 bcc_main_flow(
88 current_cdi_attest,
89 current_cdi_seal,
90 bcc,
91 input_values,
92 &mut next_cdi_values,
93 next_bcc,
94 )
95 })?;
96 Ok(OwnedDiceArtifacts { cdi_values: next_cdi_values, bcc: next_bcc })
97}
Alice Wang44f48b22023-02-09 09:51:22 +000098
99/// Executes the main DICE flow.
100///
101/// Given a full set of input values and the current CDI values, computes the
102/// next CDI values and a matching certificate.
103pub fn retry_dice_main_flow(
104 current_cdi_attest: &Cdi,
105 current_cdi_seal: &Cdi,
106 input_values: &InputValues,
107) -> Result<(CdiValues, Vec<u8>)> {
108 let mut next_cdi_values = CdiValues::default();
Alice Wangef999242023-05-22 11:14:59 +0000109 let next_cdi_certificate = retry_with_measured_buffer(|next_cdi_certificate| {
Alice Wang44f48b22023-02-09 09:51:22 +0000110 dice_main_flow(
111 current_cdi_attest,
112 current_cdi_seal,
113 input_values,
114 next_cdi_certificate,
115 &mut next_cdi_values,
116 )
117 })?;
118 Ok((next_cdi_values, next_cdi_certificate))
119}
Alice Wang4d611772023-02-13 09:45:21 +0000120
121/// Generates an X.509 certificate from the given `subject_private_key_seed` and
122/// `input_values`, and signed by `authority_private_key_seed`.
123/// The subject private key seed is supplied here so the implementation can choose
124/// between asymmetric mechanisms, for example ECDSA vs Ed25519.
125/// Returns the generated certificate.
126pub fn retry_generate_certificate(
127 subject_private_key_seed: &[u8; PRIVATE_KEY_SEED_SIZE],
128 authority_private_key_seed: &[u8; PRIVATE_KEY_SEED_SIZE],
129 input_values: &InputValues,
130) -> Result<Vec<u8>> {
Alice Wangef999242023-05-22 11:14:59 +0000131 retry_with_measured_buffer(|certificate| {
Alice Wang4d611772023-02-13 09:45:21 +0000132 generate_certificate(
133 subject_private_key_seed,
134 authority_private_key_seed,
135 input_values,
136 certificate,
137 )
138 })
139}