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 | 7b2ab94 | 2023-09-12 13:04:42 +0000 | [diff] [blame] | 18 | use super::ec_key::EcKey; |
Alice Wang | ff5592d | 2023-09-13 15:27:39 +0000 | [diff] [blame] | 19 | use super::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 | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 23 | use ciborium::{cbor, value::Value}; |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 24 | use core::result; |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 25 | use coset::{iana, AsCborValue, CoseSign1, CoseSign1Builder, HeaderBuilder}; |
Alice Wang | 6bc2a70 | 2023-09-22 12:42:13 +0000 | [diff] [blame] | 26 | use diced_open_dice::{keypair_from_seed, sign, DiceArtifacts, PrivateKey}; |
| 27 | use log::error; |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 28 | use service_vm_comm::{EcdsaP256KeyPair, GenerateCertificateRequestParams, RequestProcessingError}; |
| 29 | |
| 30 | type Result<T> = result::Result<T, RequestProcessingError>; |
Alice Wang | 33f4cae | 2023-09-05 09:27:39 +0000 | [diff] [blame] | 31 | |
Alice Wang | 77639bf | 2023-09-21 06:57:12 +0000 | [diff] [blame] | 32 | pub(super) fn generate_ecdsa_p256_key_pair( |
| 33 | _dice_artifacts: &dyn DiceArtifacts, |
| 34 | ) -> Result<EcdsaP256KeyPair> { |
Alice Wang | a78d3f0 | 2023-09-13 12:39:16 +0000 | [diff] [blame] | 35 | let hmac_key = []; |
Alice Wang | 7b2ab94 | 2023-09-12 13:04:42 +0000 | [diff] [blame] | 36 | let ec_key = EcKey::new_p256()?; |
Alice Wang | a78d3f0 | 2023-09-13 12:39:16 +0000 | [diff] [blame] | 37 | let maced_public_key = build_maced_public_key(ec_key.cose_public_key()?, &hmac_key)?; |
Alice Wang | 7b2ab94 | 2023-09-12 13:04:42 +0000 | [diff] [blame] | 38 | |
| 39 | // TODO(b/279425980): Encrypt the private key in a key blob. |
| 40 | // Remove the printing of the private key. |
| 41 | log::debug!("Private key: {:?}", ec_key.private_key()?.as_slice()); |
| 42 | |
Alice Wang | a78d3f0 | 2023-09-13 12:39:16 +0000 | [diff] [blame] | 43 | let key_pair = EcdsaP256KeyPair { maced_public_key, key_blob: Vec::new() }; |
Alice Wang | 33f4cae | 2023-09-05 09:27:39 +0000 | [diff] [blame] | 44 | Ok(key_pair) |
| 45 | } |
Alice Wang | 464e473 | 2023-09-06 12:25:22 +0000 | [diff] [blame] | 46 | |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 47 | const CSR_PAYLOAD_SCHEMA_V3: u8 = 3; |
| 48 | const AUTH_REQ_SCHEMA_V1: u8 = 1; |
| 49 | // TODO(b/300624493): Add a new certificate type for AVF CSR. |
| 50 | const CERTIFICATE_TYPE: &str = "keymint"; |
| 51 | |
| 52 | /// Builds the CSR described in: |
| 53 | /// |
| 54 | /// hardware/interfaces/security/rkp/aidl/android/hardware/security/keymint/ |
| 55 | /// generateCertificateRequestV2.cddl |
Alice Wang | 464e473 | 2023-09-06 12:25:22 +0000 | [diff] [blame] | 56 | pub(super) fn generate_certificate_request( |
Alice Wang | ff5592d | 2023-09-13 15:27:39 +0000 | [diff] [blame] | 57 | params: GenerateCertificateRequestParams, |
Alice Wang | 6bc2a70 | 2023-09-22 12:42:13 +0000 | [diff] [blame] | 58 | dice_artifacts: &dyn DiceArtifacts, |
Alice Wang | 464e473 | 2023-09-06 12:25:22 +0000 | [diff] [blame] | 59 | ) -> Result<Vec<u8>> { |
Alice Wang | ff5592d | 2023-09-13 15:27:39 +0000 | [diff] [blame] | 60 | // TODO(b/300590857): Derive the HMAC key from the DICE sealing CDI. |
| 61 | let hmac_key = []; |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 62 | let mut public_keys: Vec<Value> = Vec::new(); |
Alice Wang | ff5592d | 2023-09-13 15:27:39 +0000 | [diff] [blame] | 63 | for key_to_sign in params.keys_to_sign { |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 64 | let public_key = validate_public_key(&key_to_sign, &hmac_key)?; |
| 65 | public_keys.push(public_key.to_cbor_value()?); |
Alice Wang | ff5592d | 2023-09-13 15:27:39 +0000 | [diff] [blame] | 66 | } |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 67 | // Builds `CsrPayload`. |
| 68 | let csr_payload = cbor!([ |
| 69 | Value::Integer(CSR_PAYLOAD_SCHEMA_V3.into()), |
| 70 | Value::Text(String::from(CERTIFICATE_TYPE)), |
| 71 | // TODO(b/299256925): Add device info in CBOR format here. |
| 72 | Value::Array(public_keys), |
| 73 | ])?; |
| 74 | let csr_payload = cbor_to_vec(&csr_payload)?; |
| 75 | |
| 76 | // Builds `SignedData`. |
| 77 | let signed_data_payload = |
| 78 | cbor!([Value::Bytes(params.challenge.to_vec()), Value::Bytes(csr_payload)])?; |
Alice Wang | 6bc2a70 | 2023-09-22 12:42:13 +0000 | [diff] [blame] | 79 | 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] | 80 | |
| 81 | // Builds `AuthenticatedRequest<CsrPayload>`. |
Alice Wang | a2738b7 | 2023-09-22 15:31:28 +0000 | [diff] [blame^] | 82 | // Currently `UdsCerts` is left empty because it is only needed for Samsung devices. |
| 83 | // Check http://b/301574013#comment3 for more information. |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 84 | let uds_certs = Value::Map(Vec::new()); |
Alice Wang | a2738b7 | 2023-09-22 15:31:28 +0000 | [diff] [blame^] | 85 | let dice_cert_chain = dice_artifacts |
| 86 | .bcc() |
| 87 | .map(read_to_value) |
| 88 | .ok_or(RequestProcessingError::MissingDiceChain)??; |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 89 | let auth_req = cbor!([ |
| 90 | Value::Integer(AUTH_REQ_SCHEMA_V1.into()), |
| 91 | uds_certs, |
| 92 | dice_cert_chain, |
| 93 | signed_data, |
| 94 | ])?; |
| 95 | cbor_to_vec(&auth_req) |
| 96 | } |
| 97 | |
| 98 | /// Builds the `SignedData` for the given payload. |
Alice Wang | 6bc2a70 | 2023-09-22 12:42:13 +0000 | [diff] [blame] | 99 | fn build_signed_data(payload: &Value, dice_artifacts: &dyn DiceArtifacts) -> Result<CoseSign1> { |
| 100 | let cdi_leaf_priv = derive_cdi_leaf_priv(dice_artifacts).map_err(|e| { |
| 101 | error!("Failed to derive the CDI_Leaf_Priv: {e}"); |
| 102 | RequestProcessingError::InternalError |
| 103 | })?; |
| 104 | let signing_algorithm = iana::Algorithm::EdDSA; |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 105 | let protected = HeaderBuilder::new().algorithm(signing_algorithm).build(); |
| 106 | let signed_data = CoseSign1Builder::new() |
| 107 | .protected(protected) |
| 108 | .payload(cbor_to_vec(payload)?) |
Alice Wang | 6bc2a70 | 2023-09-22 12:42:13 +0000 | [diff] [blame] | 109 | .try_create_signature(&[], |message| sign_message(message, &cdi_leaf_priv))? |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 110 | .build(); |
| 111 | Ok(signed_data) |
| 112 | } |
| 113 | |
Alice Wang | 6bc2a70 | 2023-09-22 12:42:13 +0000 | [diff] [blame] | 114 | fn derive_cdi_leaf_priv(dice_artifacts: &dyn DiceArtifacts) -> diced_open_dice::Result<PrivateKey> { |
| 115 | let (_, private_key) = keypair_from_seed(dice_artifacts.cdi_attest())?; |
| 116 | Ok(private_key) |
| 117 | } |
| 118 | |
| 119 | fn sign_message(message: &[u8], private_key: &PrivateKey) -> Result<Vec<u8>> { |
| 120 | Ok(sign(message, private_key.as_array()) |
| 121 | .map_err(|e| { |
| 122 | error!("Failed to sign the CSR: {e}"); |
| 123 | RequestProcessingError::InternalError |
| 124 | })? |
| 125 | .to_vec()) |
Alice Wang | 464e473 | 2023-09-06 12:25:22 +0000 | [diff] [blame] | 126 | } |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 127 | |
| 128 | fn cbor_to_vec(v: &Value) -> Result<Vec<u8>> { |
| 129 | let mut data = Vec::new(); |
| 130 | ciborium::into_writer(v, &mut data).map_err(coset::CoseError::from)?; |
| 131 | Ok(data) |
| 132 | } |
Alice Wang | a2738b7 | 2023-09-22 15:31:28 +0000 | [diff] [blame^] | 133 | |
| 134 | /// Read a CBOR `Value` from a byte slice, failing if any extra data remains |
| 135 | /// after the `Value` has been read. |
| 136 | fn read_to_value(mut data: &[u8]) -> Result<Value> { |
| 137 | let value = ciborium::from_reader(&mut data).map_err(|e| { |
| 138 | error!("Failed to deserialize the data into CBOR value: {e}"); |
| 139 | RequestProcessingError::CborValueError |
| 140 | })?; |
| 141 | if data.is_empty() { |
| 142 | Ok(value) |
| 143 | } else { |
| 144 | error!("CBOR input has extra data."); |
| 145 | Err(RequestProcessingError::CborValueError) |
| 146 | } |
| 147 | } |