blob: 80953b56062d575e232af7f3ab1cba9921bf3b66 [file] [log] [blame]
Alice Wangc2fec932023-02-23 16:24:02 +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//! Handles the RKP (Remote Key Provisioning) VM and host communication.
16//! The RKP VM will be recognized and attested by the RKP server periodically and
17//! serves as a trusted platform to attest a client VM.
18
Alice Wangf3482602023-09-08 11:51:29 +000019use android_hardware_security_rkp::aidl::android::hardware::security::keymint::MacedPublicKey::MacedPublicKey;
Alice Wanga4486592023-09-05 08:25:59 +000020use anyhow::{bail, Context, Result};
Alice Wangf3482602023-09-08 11:51:29 +000021use service_vm_comm::{EcdsaP256KeyPair, GenerateCertificateRequestParams, Request, Response};
Alice Wang734801c2023-09-05 11:46:50 +000022use service_vm_manager::ServiceVm;
Alice Wangc2fec932023-02-23 16:24:02 +000023
Alice Wangc206b9b2023-08-28 14:13:51 +000024pub(crate) fn request_certificate(csr: &[u8]) -> Result<Vec<u8>> {
Alice Wanga4486592023-09-05 08:25:59 +000025 let mut vm = ServiceVm::start()?;
Alice Wangc2fec932023-02-23 16:24:02 +000026
Alice Wanga4486592023-09-05 08:25:59 +000027 // TODO(b/271275206): Send the correct request type with client VM's
28 // information to be attested.
29 let request = Request::Reverse(csr.to_vec());
Alice Wangfbdc85b2023-09-07 12:56:46 +000030 match vm.process_request(request).context("Failed to process request")? {
Alice Wanga4486592023-09-05 08:25:59 +000031 Response::Reverse(cert) => Ok(cert),
32 _ => bail!("Incorrect response type"),
33 }
Alice Wangc2fec932023-02-23 16:24:02 +000034}
Alice Wangf3482602023-09-08 11:51:29 +000035
36pub(crate) fn generate_ecdsa_p256_key_pair() -> Result<EcdsaP256KeyPair> {
37 let mut vm = ServiceVm::start()?;
38 let request = Request::GenerateEcdsaP256KeyPair;
39 match vm.process_request(request).context("Failed to process request")? {
40 Response::GenerateEcdsaP256KeyPair(key_pair) => Ok(key_pair),
41 _ => bail!("Incorrect response type"),
42 }
43}
44
45pub(crate) fn generate_certificate_request(
46 keys_to_sign: &[MacedPublicKey],
47 challenge: &[u8],
48) -> Result<Vec<u8>> {
49 let params = GenerateCertificateRequestParams {
50 keys_to_sign: keys_to_sign.iter().map(|v| v.macedKey.to_vec()).collect(),
51 challenge: challenge.to_vec(),
52 };
53 let request = Request::GenerateCertificateRequest(params);
54
55 let mut vm = ServiceVm::start()?;
56 match vm.process_request(request).context("Failed to process request")? {
57 Response::GenerateCertificateRequest(csr) => Ok(csr),
58 _ => bail!("Incorrect response type"),
59 }
60}