Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [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 | //! This module provides a set of sample input values for a DICE chain, a sample UDS, |
| 16 | //! as well as tuple of CDIs and BCC derived thereof. |
| 17 | |
Alice Wang | 4d3059a | 2023-02-15 10:24:33 +0000 | [diff] [blame] | 18 | use anyhow::{anyhow, Context, Result}; |
Alice Wang | 84dcbe1 | 2023-02-28 13:50:47 +0000 | [diff] [blame^] | 19 | use ciborium::value::Value; |
| 20 | use coset::{iana, Algorithm, CborSerializable, CoseKey, KeyOperation, KeyType, Label}; |
Alice Wang | fa83922 | 2023-02-20 09:09:22 +0000 | [diff] [blame] | 21 | use diced_open_dice::{ |
| 22 | derive_cdi_private_key_seed, keypair_from_seed, retry_bcc_format_config_descriptor, |
| 23 | retry_bcc_main_flow, retry_dice_main_flow, Config, DiceArtifacts, DiceMode, InputValues, |
| 24 | OwnedDiceArtifacts, CDI_SIZE, HASH_SIZE, HIDDEN_SIZE, |
| 25 | }; |
| 26 | use diced_utils::cbor; |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 27 | use std::ffi::CStr; |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 28 | |
| 29 | /// Sample UDS used to perform the root dice flow by `make_sample_bcc_and_cdis`. |
Alice Wang | fa83922 | 2023-02-20 09:09:22 +0000 | [diff] [blame] | 30 | pub const UDS: &[u8; CDI_SIZE] = &[ |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 31 | 0x65, 0x4f, 0xab, 0xa9, 0xa5, 0xad, 0x0f, 0x5e, 0x15, 0xc3, 0x12, 0xf7, 0x77, 0x45, 0xfa, 0x55, |
| 32 | 0x18, 0x6a, 0xa6, 0x34, 0xb6, 0x7c, 0x82, 0x7b, 0x89, 0x4c, 0xc5, 0x52, 0xd3, 0x27, 0x35, 0x8e, |
| 33 | ]; |
| 34 | |
Alice Wang | fa83922 | 2023-02-20 09:09:22 +0000 | [diff] [blame] | 35 | const CODE_HASH_ABL: [u8; HASH_SIZE] = [ |
| 36 | 0x16, 0x48, 0xf2, 0x55, 0x53, 0x23, 0xdd, 0x15, 0x2e, 0x83, 0x38, 0xc3, 0x64, 0x38, 0x63, 0x26, |
| 37 | 0x0f, 0xcf, 0x5b, 0xd1, 0x3a, 0xd3, 0x40, 0x3e, 0x23, 0xf8, 0x34, 0x4c, 0x6d, 0xa2, 0xbe, 0x25, |
| 38 | 0x1c, 0xb0, 0x29, 0xe8, 0xc3, 0xfb, 0xb8, 0x80, 0xdc, 0xb1, 0xd2, 0xb3, 0x91, 0x4d, 0xd3, 0xfb, |
| 39 | 0x01, 0x0f, 0xe4, 0xe9, 0x46, 0xa2, 0xc0, 0x26, 0x57, 0x5a, 0xba, 0x30, 0xf7, 0x15, 0x98, 0x14, |
| 40 | ]; |
| 41 | const AUTHORITY_HASH_ABL: [u8; HASH_SIZE] = [ |
| 42 | 0xf9, 0x00, 0x9d, 0xc2, 0x59, 0x09, 0xe0, 0xb6, 0x98, 0xbd, 0xe3, 0x97, 0x4a, 0xcb, 0x3c, 0xe7, |
| 43 | 0x6b, 0x24, 0xc3, 0xe4, 0x98, 0xdd, 0xa9, 0x6a, 0x41, 0x59, 0x15, 0xb1, 0x23, 0xe6, 0xc8, 0xdf, |
| 44 | 0xfb, 0x52, 0xb4, 0x52, 0xc1, 0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 0xc7, |
| 58 | ]; |
| 59 | const AUTHORITY_HASH_AVB: [u8; HASH_SIZE] = [ |
| 60 | 0xb2, 0x69, 0x05, 0x48, 0x56, 0xb5, 0xfa, 0x55, 0x6f, 0xac, 0x56, 0xd9, 0x02, 0x35, 0x2b, 0xaa, |
| 61 | 0x4c, 0xba, 0x28, 0xdd, 0x82, 0x3a, 0x86, 0xf5, 0xd4, 0xc2, 0xf1, 0xf9, 0x35, 0x7d, 0xe4, 0x43, |
| 62 | 0x13, 0xbf, 0xfe, 0xd3, 0x36, 0xd8, 0x1c, 0x12, 0x78, 0x5c, 0x9c, 0x3e, 0xf6, 0x66, 0xef, 0xab, |
| 63 | 0x3d, 0x0f, 0x89, 0xa4, 0x6f, 0xc9, 0x72, 0xee, 0x73, 0x43, 0x02, 0x8a, 0xef, 0xbc, 0x05, 0x98, |
| 64 | ]; |
| 65 | const HIDDEN_AVB: [u8; HIDDEN_SIZE] = [ |
| 66 | 0x5b, 0x3f, 0xc9, 0x6b, 0xe3, 0x95, 0x59, 0x40, 0x5e, 0x64, 0xe5, 0x64, 0x3f, 0xfd, 0x21, 0x09, |
| 67 | 0x9d, 0xf3, 0xcd, 0xc7, 0xa4, 0x2a, 0xe2, 0x97, 0xdd, 0xe2, 0x4f, 0xb0, 0x7d, 0x7e, 0xf5, 0x8e, |
| 68 | 0xd6, 0x4d, 0x84, 0x25, 0x54, 0x41, 0x3f, 0x8f, 0x78, 0x64, 0x1a, 0x51, 0x27, 0x9d, 0x55, 0x8a, |
| 69 | 0xe9, 0x90, 0x35, 0xab, 0x39, 0x80, 0x4b, 0x94, 0x40, 0x84, 0xa2, 0xfd, 0x73, 0xeb, 0x35, 0x7a, |
| 70 | ]; |
| 71 | const AUTHORITY_HASH_ANDROID: [u8; HASH_SIZE] = [ |
| 72 | 0x04, 0x25, 0x5d, 0x60, 0x5f, 0x5c, 0x45, 0x0d, 0xf2, 0x9a, 0x6e, 0x99, 0x30, 0x03, 0xb8, 0xd6, |
| 73 | 0xe1, 0x99, 0x71, 0x1b, 0xf8, 0x44, 0xfa, 0xb5, 0x31, 0x79, 0x1c, 0x37, 0x68, 0x4e, 0x1d, 0xc0, |
| 74 | 0x24, 0x74, 0x68, 0xf8, 0x80, 0x20, 0x3e, 0x44, 0xb1, 0x43, 0xd2, 0x9c, 0xfc, 0x12, 0x9e, 0x77, |
| 75 | 0x0a, 0xde, 0x29, 0x24, 0xff, 0x2e, 0xfa, 0xc7, 0x10, 0xd5, 0x73, 0xd4, 0xc6, 0xdf, 0x62, 0x9f, |
| 76 | ]; |
| 77 | |
Alice Wang | 84dcbe1 | 2023-02-28 13:50:47 +0000 | [diff] [blame^] | 78 | fn ed25519_public_key_to_cbor(public_key: &[u8]) -> Result<Vec<u8>> { |
| 79 | let key = CoseKey { |
| 80 | kty: KeyType::Assigned(iana::KeyType::OKP), |
| 81 | alg: Some(Algorithm::Assigned(iana::Algorithm::EdDSA)), |
| 82 | key_ops: vec![KeyOperation::Assigned(iana::KeyOperation::Verify)].into_iter().collect(), |
| 83 | params: vec![ |
| 84 | ( |
| 85 | Label::Int(iana::Ec2KeyParameter::Crv as i64), |
| 86 | Value::from(iana::EllipticCurve::Ed25519 as u64), |
| 87 | ), |
| 88 | (Label::Int(iana::Ec2KeyParameter::X as i64), Value::Bytes(public_key.to_vec())), |
| 89 | ], |
| 90 | ..Default::default() |
| 91 | }; |
| 92 | key.to_vec() |
| 93 | .map_err(|e| anyhow!(format!("Failed to serialize the key to CBOR data. Error: {e}"))) |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Alice Wang | fa83922 | 2023-02-20 09:09:22 +0000 | [diff] [blame] | 96 | /// Makes a DICE chain (BCC) from the sample input. |
| 97 | /// |
| 98 | /// The DICE chain is of the following format: |
| 99 | /// public key derived from UDS -> ABL certificate -> AVB certificate -> Android certificate |
| 100 | pub fn make_sample_bcc_and_cdis() -> Result<OwnedDiceArtifacts> { |
| 101 | let private_key_seed = derive_cdi_private_key_seed(UDS) |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 102 | .context("In make_sample_bcc_and_cdis: Trying to derive private key seed.")?; |
| 103 | |
Alice Wang | fa83922 | 2023-02-20 09:09:22 +0000 | [diff] [blame] | 104 | // Sets the root public key in DICE chain (BCC). |
| 105 | let (public_key, _) = keypair_from_seed(private_key_seed.as_array()) |
Alice Wang | f59662d | 2023-02-10 16:07:56 +0000 | [diff] [blame] | 106 | .context("In make_sample_bcc_and_cids: Failed to generate key pair.")?; |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 107 | let mut bcc: Vec<u8> = vec![]; |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 108 | cbor::encode_header(4 /* CBOR ARRAY */, 2, &mut bcc) |
| 109 | .context("In make_sample_bcc_and_cdis: Trying to encode array header.")?; |
Alice Wang | 84dcbe1 | 2023-02-28 13:50:47 +0000 | [diff] [blame^] | 110 | bcc.extend(ed25519_public_key_to_cbor(&public_key)?); |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 111 | |
Alice Wang | fa83922 | 2023-02-20 09:09:22 +0000 | [diff] [blame] | 112 | // Appends ABL certificate to DICE chain. |
| 113 | let config_descriptor = retry_bcc_format_config_descriptor( |
| 114 | Some(CStr::from_bytes_with_nul(b"ABL\0").unwrap()), |
| 115 | Some(1), // version |
| 116 | true, |
| 117 | )?; |
| 118 | let input_values = InputValues::new( |
| 119 | CODE_HASH_ABL, |
| 120 | Config::Descriptor(config_descriptor.as_slice()), |
| 121 | AUTHORITY_HASH_ABL, |
| 122 | DiceMode::kDiceModeNormal, |
| 123 | HIDDEN_ABL, |
| 124 | ); |
| 125 | let (cdi_values, mut cert) = retry_dice_main_flow(UDS, UDS, &input_values) |
| 126 | .context("In make_sample_bcc_and_cdis: Trying to run first main flow.")?; |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 127 | bcc.append(&mut cert); |
| 128 | |
Alice Wang | fa83922 | 2023-02-20 09:09:22 +0000 | [diff] [blame] | 129 | // Appends AVB certificate to DICE chain. |
| 130 | let config_descriptor = retry_bcc_format_config_descriptor( |
| 131 | Some(CStr::from_bytes_with_nul(b"AVB\0").unwrap()), |
| 132 | Some(1), // version |
| 133 | true, |
| 134 | )?; |
| 135 | let input_values = InputValues::new( |
| 136 | CODE_HASH_AVB, |
| 137 | Config::Descriptor(config_descriptor.as_slice()), |
| 138 | AUTHORITY_HASH_AVB, |
| 139 | DiceMode::kDiceModeNormal, |
| 140 | HIDDEN_AVB, |
| 141 | ); |
| 142 | let dice_artifacts = |
| 143 | retry_bcc_main_flow(&cdi_values.cdi_attest, &cdi_values.cdi_seal, &bcc, &input_values) |
| 144 | .context("In make_sample_bcc_and_cdis: Trying to run first bcc main flow.")?; |
| 145 | |
| 146 | // Appends Android certificate to DICE chain. |
| 147 | let config_descriptor = retry_bcc_format_config_descriptor( |
| 148 | Some(CStr::from_bytes_with_nul(b"Android\0").unwrap()), |
| 149 | Some(12), // version |
| 150 | true, |
| 151 | )?; |
| 152 | let input_values = InputValues::new( |
| 153 | [0u8; HASH_SIZE], // code_hash |
| 154 | Config::Descriptor(config_descriptor.as_slice()), |
| 155 | AUTHORITY_HASH_ANDROID, |
| 156 | DiceMode::kDiceModeNormal, |
| 157 | [0u8; HIDDEN_SIZE], // hidden |
| 158 | ); |
| 159 | retry_bcc_main_flow( |
Alice Wang | 4d3059a | 2023-02-15 10:24:33 +0000 | [diff] [blame] | 160 | dice_artifacts.cdi_attest(), |
| 161 | dice_artifacts.cdi_seal(), |
| 162 | dice_artifacts.bcc().ok_or_else(|| anyhow!("bcc is none"))?, |
Alice Wang | fa83922 | 2023-02-20 09:09:22 +0000 | [diff] [blame] | 163 | &input_values, |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame] | 164 | ) |
| 165 | .context("In make_sample_bcc_and_cdis: Trying to run second bcc main flow.") |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 168 | #[cfg(test)] |
| 169 | mod test { |
| 170 | use super::*; |
| 171 | |
| 172 | // This simple test checks if the invocation succeeds, essentially it tests |
Alice Wang | fa83922 | 2023-02-20 09:09:22 +0000 | [diff] [blame] | 173 | // if the initial bcc is accepted by `diced_open_dice::retry_bcc_main_flow`. |
Janis Danisevskis | c51dff8 | 2021-10-20 09:51:16 -0700 | [diff] [blame] | 174 | #[test] |
| 175 | fn make_sample_bcc_and_cdis_test() { |
| 176 | make_sample_bcc_and_cdis().unwrap(); |
| 177 | } |
| 178 | } |