Alice Wang | 76a36a2 | 2023-07-27 12:11:01 +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 the main API for the request processing module. |
| 16 | |
Alice Wang | 9aeb406 | 2023-10-30 14:19:38 +0000 | [diff] [blame] | 17 | use crate::client_vm; |
Alice Wang | 7b4b613 | 2023-10-05 08:00:16 +0000 | [diff] [blame] | 18 | use crate::rkp; |
Alice Wang | 76a36a2 | 2023-07-27 12:11:01 +0000 | [diff] [blame] | 19 | use alloc::vec::Vec; |
Alice Wang | 77639bf | 2023-09-21 06:57:12 +0000 | [diff] [blame] | 20 | use diced_open_dice::DiceArtifacts; |
Alice Wang | 76a36a2 | 2023-07-27 12:11:01 +0000 | [diff] [blame] | 21 | use service_vm_comm::{Request, Response}; |
| 22 | |
| 23 | /// Processes a request and returns the corresponding response. |
Alice Wang | 9eebbab | 2024-04-10 14:57:27 +0000 | [diff] [blame] | 24 | /// This function serves as the entry point for the request processing module. |
| 25 | pub fn process_request(request: Request, context: &RequestContext) -> Response { |
Alice Wang | 83c974d | 2023-10-05 12:05:36 +0000 | [diff] [blame] | 26 | match request { |
Alice Wang | 76a36a2 | 2023-07-27 12:11:01 +0000 | [diff] [blame] | 27 | Request::Reverse(v) => Response::Reverse(reverse(v)), |
Alice Wang | 9eebbab | 2024-04-10 14:57:27 +0000 | [diff] [blame] | 28 | Request::GenerateEcdsaP256KeyPair => { |
| 29 | rkp::generate_ecdsa_p256_key_pair(context.dice_artifacts) |
| 30 | .map_or_else(Response::Err, Response::GenerateEcdsaP256KeyPair) |
| 31 | } |
Alice Wang | 77639bf | 2023-09-21 06:57:12 +0000 | [diff] [blame] | 32 | Request::GenerateCertificateRequest(p) => { |
Alice Wang | 9eebbab | 2024-04-10 14:57:27 +0000 | [diff] [blame] | 33 | rkp::generate_certificate_request(p, context.dice_artifacts) |
Alice Wang | 77639bf | 2023-09-21 06:57:12 +0000 | [diff] [blame] | 34 | .map_or_else(Response::Err, Response::GenerateCertificateRequest) |
| 35 | } |
Alice Wang | 9eebbab | 2024-04-10 14:57:27 +0000 | [diff] [blame] | 36 | Request::RequestClientVmAttestation(p) => client_vm::request_attestation( |
| 37 | p, |
| 38 | context.dice_artifacts, |
| 39 | context.vendor_hashtree_root_digest, |
| 40 | ) |
| 41 | .map_or_else(Response::Err, Response::RequestClientVmAttestation), |
Alice Wang | 83c974d | 2023-10-05 12:05:36 +0000 | [diff] [blame] | 42 | } |
Alice Wang | 76a36a2 | 2023-07-27 12:11:01 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Alice Wang | 9eebbab | 2024-04-10 14:57:27 +0000 | [diff] [blame] | 45 | /// The context for the request processing. |
| 46 | /// |
| 47 | /// This struct contains the reference data used during the request processing. |
| 48 | pub struct RequestContext<'a> { |
| 49 | /// The reference DICE artifacts. |
| 50 | pub dice_artifacts: &'a dyn DiceArtifacts, |
| 51 | |
| 52 | /// The reference hash tree root digest of the vendor partition if exists. |
| 53 | pub vendor_hashtree_root_digest: Option<&'a [u8]>, |
| 54 | } |
| 55 | |
Alice Wang | 76a36a2 | 2023-07-27 12:11:01 +0000 | [diff] [blame] | 56 | fn reverse(payload: Vec<u8>) -> Vec<u8> { |
| 57 | payload.into_iter().rev().collect() |
| 58 | } |