blob: d449d05d6f6eed39a9100f20546c59dc83006245 [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 Wang7b2ab942023-09-12 13:04:42 +000018use super::ec_key::EcKey;
Alice Wangff5592d2023-09-13 15:27:39 +000019use super::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 Wangf7c0f942023-09-14 09:33:04 +000023use ciborium::{cbor, value::Value};
Alice Wangd80e99e2023-09-15 13:26:01 +000024use core::result;
Alice Wangf7c0f942023-09-14 09:33:04 +000025use coset::{iana, AsCborValue, CoseSign1, CoseSign1Builder, HeaderBuilder};
Alice Wang6bc2a702023-09-22 12:42:13 +000026use diced_open_dice::{keypair_from_seed, sign, DiceArtifacts, PrivateKey};
27use log::error;
Alice Wangd80e99e2023-09-15 13:26:01 +000028use service_vm_comm::{EcdsaP256KeyPair, GenerateCertificateRequestParams, RequestProcessingError};
29
30type Result<T> = result::Result<T, RequestProcessingError>;
Alice Wang33f4cae2023-09-05 09:27:39 +000031
Alice Wang77639bf2023-09-21 06:57:12 +000032pub(super) fn generate_ecdsa_p256_key_pair(
33 _dice_artifacts: &dyn DiceArtifacts,
34) -> Result<EcdsaP256KeyPair> {
Alice Wanga78d3f02023-09-13 12:39:16 +000035 let hmac_key = [];
Alice Wang7b2ab942023-09-12 13:04:42 +000036 let ec_key = EcKey::new_p256()?;
Alice Wanga78d3f02023-09-13 12:39:16 +000037 let maced_public_key = build_maced_public_key(ec_key.cose_public_key()?, &hmac_key)?;
Alice Wang7b2ab942023-09-12 13:04:42 +000038
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 Wanga78d3f02023-09-13 12:39:16 +000043 let key_pair = EcdsaP256KeyPair { maced_public_key, key_blob: Vec::new() };
Alice Wang33f4cae2023-09-05 09:27:39 +000044 Ok(key_pair)
45}
Alice Wang464e4732023-09-06 12:25:22 +000046
Alice Wangf7c0f942023-09-14 09:33:04 +000047const CSR_PAYLOAD_SCHEMA_V3: u8 = 3;
48const AUTH_REQ_SCHEMA_V1: u8 = 1;
49// TODO(b/300624493): Add a new certificate type for AVF CSR.
50const 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 Wang464e4732023-09-06 12:25:22 +000056pub(super) fn generate_certificate_request(
Alice Wangff5592d2023-09-13 15:27:39 +000057 params: GenerateCertificateRequestParams,
Alice Wang6bc2a702023-09-22 12:42:13 +000058 dice_artifacts: &dyn DiceArtifacts,
Alice Wang464e4732023-09-06 12:25:22 +000059) -> Result<Vec<u8>> {
Alice Wangff5592d2023-09-13 15:27:39 +000060 // TODO(b/300590857): Derive the HMAC key from the DICE sealing CDI.
61 let hmac_key = [];
Alice Wangf7c0f942023-09-14 09:33:04 +000062 let mut public_keys: Vec<Value> = Vec::new();
Alice Wangff5592d2023-09-13 15:27:39 +000063 for key_to_sign in params.keys_to_sign {
Alice Wangf7c0f942023-09-14 09:33:04 +000064 let public_key = validate_public_key(&key_to_sign, &hmac_key)?;
65 public_keys.push(public_key.to_cbor_value()?);
Alice Wangff5592d2023-09-13 15:27:39 +000066 }
Alice Wangf7c0f942023-09-14 09:33:04 +000067 // 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 Wang6bc2a702023-09-22 12:42:13 +000079 let signed_data = build_signed_data(&signed_data_payload, dice_artifacts)?.to_cbor_value()?;
Alice Wangf7c0f942023-09-14 09:33:04 +000080
81 // Builds `AuthenticatedRequest<CsrPayload>`.
82 // TODO(b/287233786): Add UdsCerts and DiceCertChain here.
83 let uds_certs = Value::Map(Vec::new());
84 let dice_cert_chain = Value::Array(Vec::new());
85 let auth_req = cbor!([
86 Value::Integer(AUTH_REQ_SCHEMA_V1.into()),
87 uds_certs,
88 dice_cert_chain,
89 signed_data,
90 ])?;
91 cbor_to_vec(&auth_req)
92}
93
94/// Builds the `SignedData` for the given payload.
Alice Wang6bc2a702023-09-22 12:42:13 +000095fn build_signed_data(payload: &Value, dice_artifacts: &dyn DiceArtifacts) -> Result<CoseSign1> {
96 let cdi_leaf_priv = derive_cdi_leaf_priv(dice_artifacts).map_err(|e| {
97 error!("Failed to derive the CDI_Leaf_Priv: {e}");
98 RequestProcessingError::InternalError
99 })?;
100 let signing_algorithm = iana::Algorithm::EdDSA;
Alice Wangf7c0f942023-09-14 09:33:04 +0000101 let protected = HeaderBuilder::new().algorithm(signing_algorithm).build();
102 let signed_data = CoseSign1Builder::new()
103 .protected(protected)
104 .payload(cbor_to_vec(payload)?)
Alice Wang6bc2a702023-09-22 12:42:13 +0000105 .try_create_signature(&[], |message| sign_message(message, &cdi_leaf_priv))?
Alice Wangf7c0f942023-09-14 09:33:04 +0000106 .build();
107 Ok(signed_data)
108}
109
Alice Wang6bc2a702023-09-22 12:42:13 +0000110fn derive_cdi_leaf_priv(dice_artifacts: &dyn DiceArtifacts) -> diced_open_dice::Result<PrivateKey> {
111 let (_, private_key) = keypair_from_seed(dice_artifacts.cdi_attest())?;
112 Ok(private_key)
113}
114
115fn sign_message(message: &[u8], private_key: &PrivateKey) -> Result<Vec<u8>> {
116 Ok(sign(message, private_key.as_array())
117 .map_err(|e| {
118 error!("Failed to sign the CSR: {e}");
119 RequestProcessingError::InternalError
120 })?
121 .to_vec())
Alice Wang464e4732023-09-06 12:25:22 +0000122}
Alice Wangf7c0f942023-09-14 09:33:04 +0000123
124fn cbor_to_vec(v: &Value) -> Result<Vec<u8>> {
125 let mut data = Vec::new();
126 ciborium::into_writer(v, &mut data).map_err(coset::CoseError::from)?;
127 Ok(data)
128}