Alice Wang | 748b032 | 2023-07-24 12:51:18 +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 requests and responses definitions exchanged |
| 16 | //! between the host and the service VM. |
| 17 | |
| 18 | use alloc::vec::Vec; |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 19 | use core::fmt; |
| 20 | use log::error; |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 21 | use serde::{Deserialize, Serialize}; |
| 22 | |
Alice Wang | 464e473 | 2023-09-06 12:25:22 +0000 | [diff] [blame] | 23 | type MacedPublicKey = Vec<u8>; |
| 24 | |
Alice Wang | fbdc85b | 2023-09-07 12:56:46 +0000 | [diff] [blame] | 25 | /// The main request type to be sent to the service VM. |
| 26 | #[derive(Clone, Debug, Serialize, Deserialize)] |
| 27 | pub enum ServiceVmRequest { |
| 28 | /// A request to be processed by the service VM. |
| 29 | /// |
| 30 | /// Each request has a corresponding response item. |
| 31 | Process(Request), |
| 32 | |
| 33 | /// Shuts down the service VM. No response is expected from it. |
| 34 | Shutdown, |
| 35 | } |
| 36 | |
| 37 | /// Represents a process request to be sent to the service VM. |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 38 | /// |
| 39 | /// Each request has a corresponding response item. |
| 40 | #[derive(Clone, Debug, Serialize, Deserialize)] |
| 41 | pub enum Request { |
| 42 | /// Reverse the order of the bytes in the provided byte array. |
| 43 | /// Currently this is only used for testing. |
| 44 | Reverse(Vec<u8>), |
Alice Wang | 33f4cae | 2023-09-05 09:27:39 +0000 | [diff] [blame] | 45 | |
| 46 | /// Generates a new ECDSA P-256 key pair that can be attested by the remote |
| 47 | /// server. |
| 48 | GenerateEcdsaP256KeyPair, |
Alice Wang | 464e473 | 2023-09-06 12:25:22 +0000 | [diff] [blame] | 49 | |
| 50 | /// Creates a certificate signing request to be sent to the |
| 51 | /// provisioning server. |
| 52 | GenerateCertificateRequest(GenerateCertificateRequestParams), |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /// Represents a response to a request sent to the service VM. |
| 56 | /// |
| 57 | /// Each response corresponds to a specific request. |
| 58 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 59 | pub enum Response { |
| 60 | /// Reverse the order of the bytes in the provided byte array. |
| 61 | Reverse(Vec<u8>), |
Alice Wang | 33f4cae | 2023-09-05 09:27:39 +0000 | [diff] [blame] | 62 | |
| 63 | /// Returns the new ECDSA P-256 key pair. |
| 64 | GenerateEcdsaP256KeyPair(EcdsaP256KeyPair), |
Alice Wang | 464e473 | 2023-09-06 12:25:22 +0000 | [diff] [blame] | 65 | |
| 66 | /// Returns a CBOR Certificate Signing Request (Csr) serialized into a byte array. |
| 67 | GenerateCertificateRequest(Vec<u8>), |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 68 | |
| 69 | /// Encountered an error during the request processing. |
| 70 | Err(RequestProcessingError), |
| 71 | } |
| 72 | |
Alice Wang | 7b2ab94 | 2023-09-12 13:04:42 +0000 | [diff] [blame] | 73 | /// BoringSSL API names. |
| 74 | #[allow(missing_docs)] |
| 75 | #[allow(non_camel_case_types)] |
| 76 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 77 | pub enum BoringSSLApiName { |
Alice Wang | a78d3f0 | 2023-09-13 12:39:16 +0000 | [diff] [blame] | 78 | BN_new, |
| 79 | BN_bn2bin_padded, |
Alice Wang | 7b2ab94 | 2023-09-12 13:04:42 +0000 | [diff] [blame] | 80 | CBB_flush, |
| 81 | CBB_len, |
| 82 | EC_KEY_check_key, |
| 83 | EC_KEY_generate_key, |
Alice Wang | a78d3f0 | 2023-09-13 12:39:16 +0000 | [diff] [blame] | 84 | EC_KEY_get0_group, |
| 85 | EC_KEY_get0_public_key, |
Alice Wang | 7b2ab94 | 2023-09-12 13:04:42 +0000 | [diff] [blame] | 86 | EC_KEY_marshal_private_key, |
| 87 | EC_KEY_new_by_curve_name, |
Alice Wang | a78d3f0 | 2023-09-13 12:39:16 +0000 | [diff] [blame] | 88 | EC_POINT_get_affine_coordinates, |
| 89 | EVP_sha256, |
| 90 | HMAC, |
Alice Wang | 7b2ab94 | 2023-09-12 13:04:42 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 93 | /// Errors related to request processing. |
| 94 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 95 | pub enum RequestProcessingError { |
| 96 | /// Failed to invoke a BoringSSL API. |
Alice Wang | 7b2ab94 | 2023-09-12 13:04:42 +0000 | [diff] [blame] | 97 | BoringSSLCallFailed(BoringSSLApiName), |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 98 | |
| 99 | /// An error happened during the interaction with coset. |
| 100 | CosetError, |
| 101 | |
Alice Wang | 6bc2a70 | 2023-09-22 12:42:13 +0000 | [diff] [blame^] | 102 | /// An unexpected internal error occurred. |
| 103 | InternalError, |
| 104 | |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 105 | /// Any key to sign lacks a valid MAC. Maps to `STATUS_INVALID_MAC`. |
| 106 | InvalidMac, |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 107 | |
| 108 | /// No payload found in a key to sign. |
| 109 | KeyToSignHasEmptyPayload, |
| 110 | |
| 111 | /// An error happened when serializing to/from a `Value`. |
| 112 | CborValueError, |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | impl fmt::Display for RequestProcessingError { |
| 116 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 117 | match self { |
| 118 | Self::BoringSSLCallFailed(api_name) => { |
Alice Wang | 7b2ab94 | 2023-09-12 13:04:42 +0000 | [diff] [blame] | 119 | write!(f, "Failed to invoke a BoringSSL API: {api_name:?}") |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 120 | } |
| 121 | Self::CosetError => write!(f, "Encountered an error with coset"), |
Alice Wang | 6bc2a70 | 2023-09-22 12:42:13 +0000 | [diff] [blame^] | 122 | Self::InternalError => write!(f, "An unexpected internal error occurred"), |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 123 | Self::InvalidMac => write!(f, "A key to sign lacks a valid MAC."), |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 124 | Self::KeyToSignHasEmptyPayload => write!(f, "No payload found in a key to sign."), |
| 125 | Self::CborValueError => { |
| 126 | write!(f, "An error happened when serializing to/from a CBOR Value.") |
| 127 | } |
Alice Wang | d80e99e | 2023-09-15 13:26:01 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | impl From<coset::CoseError> for RequestProcessingError { |
| 133 | fn from(e: coset::CoseError) -> Self { |
| 134 | error!("Coset error: {e}"); |
| 135 | Self::CosetError |
| 136 | } |
Alice Wang | 464e473 | 2023-09-06 12:25:22 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 139 | impl From<ciborium::value::Error> for RequestProcessingError { |
| 140 | fn from(e: ciborium::value::Error) -> Self { |
| 141 | error!("CborValueError: {e}"); |
| 142 | Self::CborValueError |
| 143 | } |
| 144 | } |
| 145 | |
Alice Wang | 464e473 | 2023-09-06 12:25:22 +0000 | [diff] [blame] | 146 | /// Represents the params passed to GenerateCertificateRequest |
| 147 | #[derive(Clone, Debug, Serialize, Deserialize)] |
| 148 | pub struct GenerateCertificateRequestParams { |
| 149 | /// Contains the set of keys to certify. |
| 150 | pub keys_to_sign: Vec<MacedPublicKey>, |
| 151 | |
| 152 | /// challenge contains a byte strong from the provisioning server which will be |
| 153 | /// included in the signed data of the CSR structure. |
| 154 | /// The supported sizes is between 0 and 64 bytes, inclusive. |
| 155 | pub challenge: Vec<u8>, |
Alice Wang | 33f4cae | 2023-09-05 09:27:39 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /// Represents an ECDSA P-256 key pair. |
| 159 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 160 | pub struct EcdsaP256KeyPair { |
| 161 | /// Contains a CBOR-encoded public key specified in: |
| 162 | /// |
| 163 | /// hardware/interfaces/security/rkp/aidl/android/hardware/security/keymint/MacedPublicKey.aidl |
Alice Wang | 464e473 | 2023-09-06 12:25:22 +0000 | [diff] [blame] | 164 | pub maced_public_key: MacedPublicKey, |
Alice Wang | 33f4cae | 2023-09-05 09:27:39 +0000 | [diff] [blame] | 165 | |
| 166 | /// Contains a handle to the private key. |
| 167 | pub key_blob: Vec<u8>, |
Alice Wang | 748b032 | 2023-07-24 12:51:18 +0000 | [diff] [blame] | 168 | } |