Alice Wang | 33f4cae | 2023-09-05 09:27:39 +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 contains functions related to the attestation of the |
Alice Wang | 7b2ab94 | 2023-09-12 13:04:42 +0000 | [diff] [blame] | 16 | //! service VM via the RKP (Remote Key Provisioning) server. |
Alice Wang | 33f4cae | 2023-09-05 09:27:39 +0000 | [diff] [blame] | 17 | |
Alice Wang | 8b8e6e6 | 2023-10-02 09:10:13 +0000 | [diff] [blame] | 18 | use crate::keyblob::EncryptedKeyBlob; |
Alice Wang | facc2b8 | 2023-10-05 14:05:47 +0000 | [diff] [blame] | 19 | use crate::pub_key::{build_maced_public_key, validate_public_key}; |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 20 | use alloc::string::String; |
| 21 | use alloc::vec; |
Alice Wang | 33f4cae | 2023-09-05 09:27:39 +0000 | [diff] [blame] | 22 | use alloc::vec::Vec; |
Alice Wang | b3fcf63 | 2023-09-26 08:32:55 +0000 | [diff] [blame] | 23 | use bssl_avf::EcKey; |
Alice Wang | 0a76da3 | 2024-03-05 13:41:27 +0000 | [diff] [blame] | 24 | use ciborium::{ |
| 25 | cbor, |
| 26 | value::{CanonicalValue, Value}, |
| 27 | }; |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 28 | use core::result; |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 29 | use coset::{iana, AsCborValue, CoseSign1, CoseSign1Builder, HeaderBuilder}; |
Alice Wang | 637b197 | 2023-10-12 15:12:43 +0000 | [diff] [blame] | 30 | use diced_open_dice::{derive_cdi_leaf_priv, kdf, sign, DiceArtifacts, PrivateKey}; |
Alice Wang | 6bc2a70 | 2023-09-22 12:42:13 +0000 | [diff] [blame] | 31 | use log::error; |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 32 | use service_vm_comm::{EcdsaP256KeyPair, GenerateCertificateRequestParams, RequestProcessingError}; |
Alice Wang | 4ca86b6 | 2023-09-22 11:49:43 +0000 | [diff] [blame] | 33 | use zeroize::Zeroizing; |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 34 | |
| 35 | type Result<T> = result::Result<T, RequestProcessingError>; |
Alice Wang | 33f4cae | 2023-09-05 09:27:39 +0000 | [diff] [blame] | 36 | |
Alice Wang | 4ca86b6 | 2023-09-22 11:49:43 +0000 | [diff] [blame] | 37 | /// The salt is generated randomly with: |
| 38 | /// hexdump -vn32 -e'16/1 "0x%02X, " 1 "\n"' /dev/urandom |
| 39 | const HMAC_KEY_SALT: [u8; 32] = [ |
| 40 | 0x82, 0x80, 0xFA, 0xD3, 0xA8, 0x0A, 0x9A, 0x4B, 0xF7, 0xA5, 0x7D, 0x7B, 0xE9, 0xC3, 0xAB, 0x13, |
| 41 | 0x89, 0xDC, 0x7B, 0x46, 0xEE, 0x71, 0x22, 0xB4, 0x5F, 0x4C, 0x3F, 0xE2, 0x40, 0x04, 0x3B, 0x6C, |
| 42 | ]; |
Alice Wang | 8b8e6e6 | 2023-10-02 09:10:13 +0000 | [diff] [blame] | 43 | const HMAC_KEY_INFO: &[u8] = b"rialto hmac wkey"; |
Alice Wang | 4ca86b6 | 2023-09-22 11:49:43 +0000 | [diff] [blame] | 44 | const HMAC_KEY_LENGTH: usize = 32; |
| 45 | |
Alice Wang | 77639bf | 2023-09-21 06:57:12 +0000 | [diff] [blame] | 46 | pub(super) fn generate_ecdsa_p256_key_pair( |
Alice Wang | 4ca86b6 | 2023-09-22 11:49:43 +0000 | [diff] [blame] | 47 | dice_artifacts: &dyn DiceArtifacts, |
Alice Wang | 77639bf | 2023-09-21 06:57:12 +0000 | [diff] [blame] | 48 | ) -> Result<EcdsaP256KeyPair> { |
Alice Wang | 4ca86b6 | 2023-09-22 11:49:43 +0000 | [diff] [blame] | 49 | let hmac_key = derive_hmac_key(dice_artifacts)?; |
Alice Wang | 9bd9809 | 2023-11-10 14:08:12 +0000 | [diff] [blame] | 50 | let mut ec_key = EcKey::new_p256()?; |
| 51 | ec_key.generate_key()?; |
Alice Wang | 8b8e6e6 | 2023-10-02 09:10:13 +0000 | [diff] [blame] | 52 | |
Alice Wang | 4ca86b6 | 2023-09-22 11:49:43 +0000 | [diff] [blame] | 53 | let maced_public_key = build_maced_public_key(ec_key.cose_public_key()?, hmac_key.as_ref())?; |
Alice Wang | 8b8e6e6 | 2023-10-02 09:10:13 +0000 | [diff] [blame] | 54 | let key_blob = |
Alice Wang | 000595b | 2023-10-02 13:46:45 +0000 | [diff] [blame] | 55 | EncryptedKeyBlob::new(ec_key.ec_private_key()?.as_slice(), dice_artifacts.cdi_seal())?; |
Alice Wang | 7b2ab94 | 2023-09-12 13:04:42 +0000 | [diff] [blame] | 56 | |
Alice Wang | 5dddeea | 2023-10-13 12:56:22 +0000 | [diff] [blame] | 57 | let key_pair = |
| 58 | EcdsaP256KeyPair { maced_public_key, key_blob: cbor_util::serialize(&key_blob)? }; |
Alice Wang | 33f4cae | 2023-09-05 09:27:39 +0000 | [diff] [blame] | 59 | Ok(key_pair) |
| 60 | } |
Alice Wang | 464e473 | 2023-09-06 12:25:22 +0000 | [diff] [blame] | 61 | |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 62 | const CSR_PAYLOAD_SCHEMA_V3: u8 = 3; |
| 63 | const AUTH_REQ_SCHEMA_V1: u8 = 1; |
| 64 | // TODO(b/300624493): Add a new certificate type for AVF CSR. |
| 65 | const CERTIFICATE_TYPE: &str = "keymint"; |
| 66 | |
| 67 | /// Builds the CSR described in: |
| 68 | /// |
| 69 | /// hardware/interfaces/security/rkp/aidl/android/hardware/security/keymint/ |
| 70 | /// generateCertificateRequestV2.cddl |
Alice Wang | 464e473 | 2023-09-06 12:25:22 +0000 | [diff] [blame] | 71 | pub(super) fn generate_certificate_request( |
Alice Wang | ff5592d | 2023-09-13 15:27:39 +0000 | [diff] [blame] | 72 | params: GenerateCertificateRequestParams, |
Alice Wang | 6bc2a70 | 2023-09-22 12:42:13 +0000 | [diff] [blame] | 73 | dice_artifacts: &dyn DiceArtifacts, |
Alice Wang | 464e473 | 2023-09-06 12:25:22 +0000 | [diff] [blame] | 74 | ) -> Result<Vec<u8>> { |
Alice Wang | 4ca86b6 | 2023-09-22 11:49:43 +0000 | [diff] [blame] | 75 | let hmac_key = derive_hmac_key(dice_artifacts)?; |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 76 | let mut public_keys: Vec<Value> = Vec::new(); |
Alice Wang | ff5592d | 2023-09-13 15:27:39 +0000 | [diff] [blame] | 77 | for key_to_sign in params.keys_to_sign { |
Alice Wang | 4ca86b6 | 2023-09-22 11:49:43 +0000 | [diff] [blame] | 78 | let public_key = validate_public_key(&key_to_sign, hmac_key.as_ref())?; |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 79 | public_keys.push(public_key.to_cbor_value()?); |
Alice Wang | ff5592d | 2023-09-13 15:27:39 +0000 | [diff] [blame] | 80 | } |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 81 | // Builds `CsrPayload`. |
| 82 | let csr_payload = cbor!([ |
| 83 | Value::Integer(CSR_PAYLOAD_SCHEMA_V3.into()), |
| 84 | Value::Text(String::from(CERTIFICATE_TYPE)), |
Alice Wang | 68d1140 | 2024-01-02 13:59:44 +0000 | [diff] [blame] | 85 | device_info(), |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 86 | Value::Array(public_keys), |
| 87 | ])?; |
Alice Wang | 5dddeea | 2023-10-13 12:56:22 +0000 | [diff] [blame] | 88 | let csr_payload = cbor_util::serialize(&csr_payload)?; |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 89 | |
| 90 | // Builds `SignedData`. |
| 91 | let signed_data_payload = |
| 92 | cbor!([Value::Bytes(params.challenge.to_vec()), Value::Bytes(csr_payload)])?; |
Alice Wang | 6bc2a70 | 2023-09-22 12:42:13 +0000 | [diff] [blame] | 93 | let signed_data = build_signed_data(&signed_data_payload, dice_artifacts)?.to_cbor_value()?; |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 94 | |
| 95 | // Builds `AuthenticatedRequest<CsrPayload>`. |
Alice Wang | a2738b7 | 2023-09-22 15:31:28 +0000 | [diff] [blame] | 96 | // Currently `UdsCerts` is left empty because it is only needed for Samsung devices. |
| 97 | // Check http://b/301574013#comment3 for more information. |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 98 | let uds_certs = Value::Map(Vec::new()); |
Alice Wang | facc2b8 | 2023-10-05 14:05:47 +0000 | [diff] [blame] | 99 | let dice_cert_chain = dice_artifacts.bcc().ok_or(RequestProcessingError::MissingDiceChain)?; |
Alice Wang | 5dddeea | 2023-10-13 12:56:22 +0000 | [diff] [blame] | 100 | let dice_cert_chain: Value = cbor_util::deserialize(dice_cert_chain)?; |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 101 | let auth_req = cbor!([ |
| 102 | Value::Integer(AUTH_REQ_SCHEMA_V1.into()), |
| 103 | uds_certs, |
| 104 | dice_cert_chain, |
| 105 | signed_data, |
| 106 | ])?; |
Alice Wang | 5dddeea | 2023-10-13 12:56:22 +0000 | [diff] [blame] | 107 | Ok(cbor_util::serialize(&auth_req)?) |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Alice Wang | 68d1140 | 2024-01-02 13:59:44 +0000 | [diff] [blame] | 110 | /// Generates the device info required by the RKP server as a temporary placeholder. |
| 111 | /// More details in b/301592917. |
Alice Wang | 0a76da3 | 2024-03-05 13:41:27 +0000 | [diff] [blame] | 112 | /// |
| 113 | /// The keys of the map should be in the length-first core deterministic encoding order |
| 114 | /// as per RFC8949. |
| 115 | fn device_info() -> CanonicalValue { |
| 116 | cbor!({ |
| 117 | "brand" => "aosp-avf", |
| 118 | "fused" => 1, |
| 119 | "model" => "avf", |
| 120 | "device" => "avf", |
| 121 | "product" => "avf", |
| 122 | "manufacturer" => "aosp-avf", |
| 123 | "vbmeta_digest" => Value::Bytes(vec![0u8; 0]), |
| 124 | "boot_patch_level" => 20240202, |
| 125 | "system_patch_level" => 202402, |
| 126 | "vendor_patch_level" => 20240202, |
| 127 | }) |
Alice Wang | 68d1140 | 2024-01-02 13:59:44 +0000 | [diff] [blame] | 128 | .unwrap() |
Alice Wang | 0a76da3 | 2024-03-05 13:41:27 +0000 | [diff] [blame] | 129 | .into() |
Alice Wang | 68d1140 | 2024-01-02 13:59:44 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Alice Wang | 4ca86b6 | 2023-09-22 11:49:43 +0000 | [diff] [blame] | 132 | fn derive_hmac_key(dice_artifacts: &dyn DiceArtifacts) -> Result<Zeroizing<[u8; HMAC_KEY_LENGTH]>> { |
| 133 | let mut key = Zeroizing::new([0u8; HMAC_KEY_LENGTH]); |
| 134 | kdf(dice_artifacts.cdi_seal(), &HMAC_KEY_SALT, HMAC_KEY_INFO, key.as_mut()).map_err(|e| { |
| 135 | error!("Failed to compute the HMAC key: {e}"); |
| 136 | RequestProcessingError::InternalError |
| 137 | })?; |
| 138 | Ok(key) |
| 139 | } |
| 140 | |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 141 | /// Builds the `SignedData` for the given payload. |
Alice Wang | 6bc2a70 | 2023-09-22 12:42:13 +0000 | [diff] [blame] | 142 | fn build_signed_data(payload: &Value, dice_artifacts: &dyn DiceArtifacts) -> Result<CoseSign1> { |
| 143 | let cdi_leaf_priv = derive_cdi_leaf_priv(dice_artifacts).map_err(|e| { |
| 144 | error!("Failed to derive the CDI_Leaf_Priv: {e}"); |
| 145 | RequestProcessingError::InternalError |
| 146 | })?; |
| 147 | let signing_algorithm = iana::Algorithm::EdDSA; |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 148 | let protected = HeaderBuilder::new().algorithm(signing_algorithm).build(); |
| 149 | let signed_data = CoseSign1Builder::new() |
| 150 | .protected(protected) |
Alice Wang | 5dddeea | 2023-10-13 12:56:22 +0000 | [diff] [blame] | 151 | .payload(cbor_util::serialize(payload)?) |
Alice Wang | 6bc2a70 | 2023-09-22 12:42:13 +0000 | [diff] [blame] | 152 | .try_create_signature(&[], |message| sign_message(message, &cdi_leaf_priv))? |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 153 | .build(); |
| 154 | Ok(signed_data) |
| 155 | } |
| 156 | |
Alice Wang | 6bc2a70 | 2023-09-22 12:42:13 +0000 | [diff] [blame] | 157 | fn sign_message(message: &[u8], private_key: &PrivateKey) -> Result<Vec<u8>> { |
| 158 | Ok(sign(message, private_key.as_array()) |
| 159 | .map_err(|e| { |
| 160 | error!("Failed to sign the CSR: {e}"); |
| 161 | RequestProcessingError::InternalError |
| 162 | })? |
| 163 | .to_vec()) |
Alice Wang | 464e473 | 2023-09-06 12:25:22 +0000 | [diff] [blame] | 164 | } |
Alice Wang | 0a76da3 | 2024-03-05 13:41:27 +0000 | [diff] [blame] | 165 | |
| 166 | #[cfg(test)] |
| 167 | mod tests { |
| 168 | use super::*; |
| 169 | |
| 170 | /// The keys of device info map should be in the length-first core deterministic encoding |
| 171 | /// order as per RFC8949. |
| 172 | /// The CBOR ordering rules are: |
| 173 | /// 1. If two keys have different lengths, the shorter one sorts earlier; |
| 174 | /// 2. If two keys have the same length, the one with the lower value in |
| 175 | /// (bytewise) lexical order sorts earlier. |
| 176 | #[test] |
| 177 | fn device_info_is_in_length_first_deterministic_order() { |
| 178 | let device_info = cbor!(device_info()).unwrap(); |
| 179 | let device_info_map = device_info.as_map().unwrap(); |
| 180 | let device_info_keys: Vec<&str> = |
| 181 | device_info_map.iter().map(|k| k.0.as_text().unwrap()).collect(); |
| 182 | let mut sorted_keys = device_info_keys.clone(); |
| 183 | sorted_keys.sort_by(|a, b| a.len().cmp(&b.len()).then(a.cmp(b))); |
| 184 | assert_eq!(device_info_keys, sorted_keys); |
| 185 | } |
| 186 | } |