blob: f5b9203de69322cf851c6d4e1ac0b3c864fcd196 [file] [log] [blame]
Alice Wang33f4cae2023-09-05 09:27:39 +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 contains functions related to the attestation of the
Alice Wang7b2ab942023-09-12 13:04:42 +000016//! service VM via the RKP (Remote Key Provisioning) server.
Alice Wang33f4cae2023-09-05 09:27:39 +000017
Alice Wang8b8e6e62023-10-02 09:10:13 +000018use crate::keyblob::EncryptedKeyBlob;
Alice Wangfacc2b82023-10-05 14:05:47 +000019use crate::pub_key::{build_maced_public_key, validate_public_key};
Alice Wangf7c0f942023-09-14 09:33:04 +000020use alloc::string::String;
21use alloc::vec;
Alice Wang33f4cae2023-09-05 09:27:39 +000022use alloc::vec::Vec;
Alice Wangb3fcf632023-09-26 08:32:55 +000023use bssl_avf::EcKey;
Alice Wangf7c0f942023-09-14 09:33:04 +000024use ciborium::{cbor, value::Value};
Alice Wangd80e99e2023-09-15 13:26:01 +000025use core::result;
Alice Wangf7c0f942023-09-14 09:33:04 +000026use coset::{iana, AsCborValue, CoseSign1, CoseSign1Builder, HeaderBuilder};
Alice Wang637b1972023-10-12 15:12:43 +000027use diced_open_dice::{derive_cdi_leaf_priv, kdf, sign, DiceArtifacts, PrivateKey};
Alice Wang6bc2a702023-09-22 12:42:13 +000028use log::error;
Alice Wangd80e99e2023-09-15 13:26:01 +000029use service_vm_comm::{EcdsaP256KeyPair, GenerateCertificateRequestParams, RequestProcessingError};
Alice Wang4ca86b62023-09-22 11:49:43 +000030use zeroize::Zeroizing;
Alice Wangd80e99e2023-09-15 13:26:01 +000031
32type Result<T> = result::Result<T, RequestProcessingError>;
Alice Wang33f4cae2023-09-05 09:27:39 +000033
Alice Wang4ca86b62023-09-22 11:49:43 +000034/// The salt is generated randomly with:
35/// hexdump -vn32 -e'16/1 "0x%02X, " 1 "\n"' /dev/urandom
36const HMAC_KEY_SALT: [u8; 32] = [
37 0x82, 0x80, 0xFA, 0xD3, 0xA8, 0x0A, 0x9A, 0x4B, 0xF7, 0xA5, 0x7D, 0x7B, 0xE9, 0xC3, 0xAB, 0x13,
38 0x89, 0xDC, 0x7B, 0x46, 0xEE, 0x71, 0x22, 0xB4, 0x5F, 0x4C, 0x3F, 0xE2, 0x40, 0x04, 0x3B, 0x6C,
39];
Alice Wang8b8e6e62023-10-02 09:10:13 +000040const HMAC_KEY_INFO: &[u8] = b"rialto hmac wkey";
Alice Wang4ca86b62023-09-22 11:49:43 +000041const HMAC_KEY_LENGTH: usize = 32;
42
Alice Wang77639bf2023-09-21 06:57:12 +000043pub(super) fn generate_ecdsa_p256_key_pair(
Alice Wang4ca86b62023-09-22 11:49:43 +000044 dice_artifacts: &dyn DiceArtifacts,
Alice Wang77639bf2023-09-21 06:57:12 +000045) -> Result<EcdsaP256KeyPair> {
Alice Wang4ca86b62023-09-22 11:49:43 +000046 let hmac_key = derive_hmac_key(dice_artifacts)?;
Alice Wang7b2ab942023-09-12 13:04:42 +000047 let ec_key = EcKey::new_p256()?;
Alice Wang8b8e6e62023-10-02 09:10:13 +000048
Alice Wang4ca86b62023-09-22 11:49:43 +000049 let maced_public_key = build_maced_public_key(ec_key.cose_public_key()?, hmac_key.as_ref())?;
Alice Wang8b8e6e62023-10-02 09:10:13 +000050 let key_blob =
51 EncryptedKeyBlob::new(ec_key.private_key()?.as_slice(), dice_artifacts.cdi_seal())?;
Alice Wang7b2ab942023-09-12 13:04:42 +000052
Alice Wang5dddeea2023-10-13 12:56:22 +000053 let key_pair =
54 EcdsaP256KeyPair { maced_public_key, key_blob: cbor_util::serialize(&key_blob)? };
Alice Wang33f4cae2023-09-05 09:27:39 +000055 Ok(key_pair)
56}
Alice Wang464e4732023-09-06 12:25:22 +000057
Alice Wangf7c0f942023-09-14 09:33:04 +000058const CSR_PAYLOAD_SCHEMA_V3: u8 = 3;
59const AUTH_REQ_SCHEMA_V1: u8 = 1;
60// TODO(b/300624493): Add a new certificate type for AVF CSR.
61const CERTIFICATE_TYPE: &str = "keymint";
62
63/// Builds the CSR described in:
64///
65/// hardware/interfaces/security/rkp/aidl/android/hardware/security/keymint/
66/// generateCertificateRequestV2.cddl
Alice Wang464e4732023-09-06 12:25:22 +000067pub(super) fn generate_certificate_request(
Alice Wangff5592d2023-09-13 15:27:39 +000068 params: GenerateCertificateRequestParams,
Alice Wang6bc2a702023-09-22 12:42:13 +000069 dice_artifacts: &dyn DiceArtifacts,
Alice Wang464e4732023-09-06 12:25:22 +000070) -> Result<Vec<u8>> {
Alice Wang4ca86b62023-09-22 11:49:43 +000071 let hmac_key = derive_hmac_key(dice_artifacts)?;
Alice Wangf7c0f942023-09-14 09:33:04 +000072 let mut public_keys: Vec<Value> = Vec::new();
Alice Wangff5592d2023-09-13 15:27:39 +000073 for key_to_sign in params.keys_to_sign {
Alice Wang4ca86b62023-09-22 11:49:43 +000074 let public_key = validate_public_key(&key_to_sign, hmac_key.as_ref())?;
Alice Wangf7c0f942023-09-14 09:33:04 +000075 public_keys.push(public_key.to_cbor_value()?);
Alice Wangff5592d2023-09-13 15:27:39 +000076 }
Alice Wangf7c0f942023-09-14 09:33:04 +000077 // Builds `CsrPayload`.
Alice Wangfcbf5ff2023-10-09 09:25:50 +000078 // TODO(b/299256925): The device information is currently empty as we do not
79 // have sufficient details to include.
80 let device_info = Value::Map(Vec::new());
Alice Wangf7c0f942023-09-14 09:33:04 +000081 let csr_payload = cbor!([
82 Value::Integer(CSR_PAYLOAD_SCHEMA_V3.into()),
83 Value::Text(String::from(CERTIFICATE_TYPE)),
Alice Wangfcbf5ff2023-10-09 09:25:50 +000084 device_info,
Alice Wangf7c0f942023-09-14 09:33:04 +000085 Value::Array(public_keys),
86 ])?;
Alice Wang5dddeea2023-10-13 12:56:22 +000087 let csr_payload = cbor_util::serialize(&csr_payload)?;
Alice Wangf7c0f942023-09-14 09:33:04 +000088
89 // Builds `SignedData`.
90 let signed_data_payload =
91 cbor!([Value::Bytes(params.challenge.to_vec()), Value::Bytes(csr_payload)])?;
Alice Wang6bc2a702023-09-22 12:42:13 +000092 let signed_data = build_signed_data(&signed_data_payload, dice_artifacts)?.to_cbor_value()?;
Alice Wangf7c0f942023-09-14 09:33:04 +000093
94 // Builds `AuthenticatedRequest<CsrPayload>`.
Alice Wanga2738b72023-09-22 15:31:28 +000095 // Currently `UdsCerts` is left empty because it is only needed for Samsung devices.
96 // Check http://b/301574013#comment3 for more information.
Alice Wangf7c0f942023-09-14 09:33:04 +000097 let uds_certs = Value::Map(Vec::new());
Alice Wangfacc2b82023-10-05 14:05:47 +000098 let dice_cert_chain = dice_artifacts.bcc().ok_or(RequestProcessingError::MissingDiceChain)?;
Alice Wang5dddeea2023-10-13 12:56:22 +000099 let dice_cert_chain: Value = cbor_util::deserialize(dice_cert_chain)?;
Alice Wangf7c0f942023-09-14 09:33:04 +0000100 let auth_req = cbor!([
101 Value::Integer(AUTH_REQ_SCHEMA_V1.into()),
102 uds_certs,
103 dice_cert_chain,
104 signed_data,
105 ])?;
Alice Wang5dddeea2023-10-13 12:56:22 +0000106 Ok(cbor_util::serialize(&auth_req)?)
Alice Wangf7c0f942023-09-14 09:33:04 +0000107}
108
Alice Wang4ca86b62023-09-22 11:49:43 +0000109fn derive_hmac_key(dice_artifacts: &dyn DiceArtifacts) -> Result<Zeroizing<[u8; HMAC_KEY_LENGTH]>> {
110 let mut key = Zeroizing::new([0u8; HMAC_KEY_LENGTH]);
111 kdf(dice_artifacts.cdi_seal(), &HMAC_KEY_SALT, HMAC_KEY_INFO, key.as_mut()).map_err(|e| {
112 error!("Failed to compute the HMAC key: {e}");
113 RequestProcessingError::InternalError
114 })?;
115 Ok(key)
116}
117
Alice Wangf7c0f942023-09-14 09:33:04 +0000118/// Builds the `SignedData` for the given payload.
Alice Wang6bc2a702023-09-22 12:42:13 +0000119fn build_signed_data(payload: &Value, dice_artifacts: &dyn DiceArtifacts) -> Result<CoseSign1> {
120 let cdi_leaf_priv = derive_cdi_leaf_priv(dice_artifacts).map_err(|e| {
121 error!("Failed to derive the CDI_Leaf_Priv: {e}");
122 RequestProcessingError::InternalError
123 })?;
124 let signing_algorithm = iana::Algorithm::EdDSA;
Alice Wangf7c0f942023-09-14 09:33:04 +0000125 let protected = HeaderBuilder::new().algorithm(signing_algorithm).build();
126 let signed_data = CoseSign1Builder::new()
127 .protected(protected)
Alice Wang5dddeea2023-10-13 12:56:22 +0000128 .payload(cbor_util::serialize(payload)?)
Alice Wang6bc2a702023-09-22 12:42:13 +0000129 .try_create_signature(&[], |message| sign_message(message, &cdi_leaf_priv))?
Alice Wangf7c0f942023-09-14 09:33:04 +0000130 .build();
131 Ok(signed_data)
132}
133
Alice Wang6bc2a702023-09-22 12:42:13 +0000134fn sign_message(message: &[u8], private_key: &PrivateKey) -> Result<Vec<u8>> {
135 Ok(sign(message, private_key.as_array())
136 .map_err(|e| {
137 error!("Failed to sign the CSR: {e}");
138 RequestProcessingError::InternalError
139 })?
140 .to_vec())
Alice Wang464e4732023-09-06 12:25:22 +0000141}