Shikha Panwar | 95084df | 2023-07-22 11:47:45 +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 | //! Class for encapsulating & managing represent VM secrets. |
| 16 | |
Shikha Panwar | f18ffc3 | 2024-03-04 16:57:02 +0000 | [diff] [blame] | 17 | use anyhow::{anyhow, ensure, Context, Result}; |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 18 | use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::IVirtualMachineService; |
| 19 | use android_hardware_security_secretkeeper::aidl::android::hardware::security::secretkeeper::ISecretkeeper::ISecretkeeper; |
| 20 | use secretkeeper_comm::data_types::request::Request; |
| 21 | use binder::{Strong}; |
Shikha Panwar | 14abe44 | 2024-02-23 15:04:27 +0000 | [diff] [blame] | 22 | use coset::{CoseKey, CborSerializable, CborOrdering}; |
Shikha Panwar | 101ac8f | 2024-01-19 11:20:18 +0000 | [diff] [blame] | 23 | use dice_policy_builder::{CertIndex, ConstraintSpec, ConstraintType, policy_for_dice_chain, MissingAction, WILDCARD_FULL_ARRAY}; |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 24 | use diced_open_dice::{DiceArtifacts, OwnedDiceArtifacts}; |
| 25 | use keystore2_crypto::ZVec; |
| 26 | use openssl::hkdf::hkdf; |
| 27 | use openssl::md::Md; |
| 28 | use openssl::sha; |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 29 | use secretkeeper_client::dice::OwnedDiceArtifactsWithExplicitKey; |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 30 | use secretkeeper_client::SkSession; |
| 31 | use secretkeeper_comm::data_types::{Id, ID_SIZE, Secret, SECRET_SIZE}; |
| 32 | use secretkeeper_comm::data_types::response::Response; |
| 33 | use secretkeeper_comm::data_types::packet::{ResponsePacket, ResponseType}; |
| 34 | use secretkeeper_comm::data_types::request_response_impl::{ |
| 35 | StoreSecretRequest, GetSecretResponse, GetSecretRequest}; |
| 36 | use secretkeeper_comm::data_types::error::SecretkeeperError; |
Shikha Panwar | 14abe44 | 2024-02-23 15:04:27 +0000 | [diff] [blame] | 37 | use std::fs; |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 38 | use zeroize::Zeroizing; |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 39 | |
Shikha Panwar | 3d3a70a | 2023-08-21 20:02:08 +0000 | [diff] [blame] | 40 | const ENCRYPTEDSTORE_KEY_IDENTIFIER: &str = "encryptedstore_key"; |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 41 | const AUTHORITY_HASH: i64 = -4670549; |
| 42 | const MODE: i64 = -4670551; |
| 43 | const CONFIG_DESC: i64 = -4670548; |
| 44 | const SECURITY_VERSION: i64 = -70005; |
Shikha Panwar | 101ac8f | 2024-01-19 11:20:18 +0000 | [diff] [blame] | 45 | const SUBCOMPONENT_DESCRIPTORS: i64 = -71002; |
| 46 | const SUBCOMPONENT_SECURITY_VERSION: i64 = 2; |
| 47 | const SUBCOMPONENT_AUTHORITY_HASH: i64 = 4; |
| 48 | // Index of DiceChainEntry corresponding to Payload (relative to the end considering DICE Chain |
| 49 | // as an array) |
| 50 | const PAYLOAD_INDEX_FROM_END: usize = 0; |
Shikha Panwar | 3d3a70a | 2023-08-21 20:02:08 +0000 | [diff] [blame] | 51 | |
Shikha Panwar | 3d3a70a | 2023-08-21 20:02:08 +0000 | [diff] [blame] | 52 | // Generated using hexdump -vn32 -e'14/1 "0x%02X, " 1 "\n"' /dev/urandom |
| 53 | const SALT_ENCRYPTED_STORE: &[u8] = &[ |
| 54 | 0xFC, 0x1D, 0x35, 0x7B, 0x96, 0xF3, 0xEF, 0x17, 0x78, 0x7D, 0x70, 0xED, 0xEA, 0xFE, 0x1D, 0x6F, |
| 55 | 0xB3, 0xF9, 0x40, 0xCE, 0xDD, 0x99, 0x40, 0xAA, 0xA7, 0x0E, 0x92, 0x73, 0x90, 0x86, 0x4A, 0x75, |
| 56 | ]; |
| 57 | const SALT_PAYLOAD_SERVICE: &[u8] = &[ |
| 58 | 0x8B, 0x0F, 0xF0, 0xD3, 0xB1, 0x69, 0x2B, 0x95, 0x84, 0x2C, 0x9E, 0x3C, 0x99, 0x56, 0x7A, 0x22, |
| 59 | 0x55, 0xF8, 0x08, 0x23, 0x81, 0x5F, 0xF5, 0x16, 0x20, 0x3E, 0xBE, 0xBA, 0xB7, 0xA8, 0x43, 0x92, |
| 60 | ]; |
| 61 | |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 62 | pub enum VmSecret { |
| 63 | // V2 secrets are derived from 2 independently secured secrets: |
| 64 | // 1. Secretkeeper protected secrets (skp secret). |
| 65 | // 2. Dice Sealing CDIs (Similar to V1). |
| 66 | // |
| 67 | // These are protected against rollback of boot images i.e. VM instance rebooted |
| 68 | // with downgraded images will not have access to VM's secret. |
| 69 | // V2 secrets require hardware support - Secretkeeper HAL, which (among other things) |
| 70 | // is backed by tamper-evident storage, providing rollback protection to these secrets. |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 71 | V2 { dice_artifacts: OwnedDiceArtifactsWithExplicitKey, skp_secret: ZVec }, |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 72 | // V1 secrets are not protected against rollback of boot images. |
| 73 | // They are reliable only if rollback of images was prevented by verified boot ie, |
| 74 | // each stage (including pvmfw/Microdroid/Microdroid Manager) prevents downgrade of next |
| 75 | // stage. These are now legacy secrets & used only when Secretkeeper HAL is not supported |
| 76 | // by device. |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 77 | V1 { dice_artifacts: OwnedDiceArtifacts }, |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Shikha Panwar | 14abe44 | 2024-02-23 15:04:27 +0000 | [diff] [blame] | 80 | // For supporting V2 secrets, guest expects the public key to be present in the Linux device tree. |
| 81 | fn get_secretkeeper_identity() -> Result<CoseKey> { |
| 82 | let key = fs::read(super::SECRETKEEPER_KEY)?; |
| 83 | let mut key = CoseKey::from_slice(&key)?; |
| 84 | key.canonicalize(CborOrdering::Lexicographic); |
| 85 | Ok(key) |
| 86 | } |
| 87 | |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 88 | impl VmSecret { |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 89 | pub fn new( |
| 90 | dice_artifacts: OwnedDiceArtifacts, |
| 91 | vm_service: &Strong<dyn IVirtualMachineService>, |
Shikha Panwar | 0503cb0 | 2024-01-05 10:11:28 +0000 | [diff] [blame] | 92 | ) -> Result<Self> { |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 93 | ensure!(dice_artifacts.bcc().is_some(), "Dice chain missing"); |
Shikha Panwar | e45e942 | 2024-02-28 21:18:10 +0000 | [diff] [blame^] | 94 | if !crate::should_defer_rollback_protection() { |
Shikha Panwar | 0503cb0 | 2024-01-05 10:11:28 +0000 | [diff] [blame] | 95 | return Ok(Self::V1 { dice_artifacts }); |
Shikha Panwar | e45e942 | 2024-02-28 21:18:10 +0000 | [diff] [blame^] | 96 | } |
Shikha Panwar | 14abe44 | 2024-02-23 15:04:27 +0000 | [diff] [blame] | 97 | |
Shikha Panwar | f18ffc3 | 2024-03-04 16:57:02 +0000 | [diff] [blame] | 98 | let explicit_dice = OwnedDiceArtifactsWithExplicitKey::from_owned_artifacts(dice_artifacts) |
| 99 | .context("Failed to get Dice artifacts in explicit key format")?; |
Shikha Panwar | 14abe44 | 2024-02-23 15:04:27 +0000 | [diff] [blame] | 100 | // For pVM, skp_secret are stored in Secretkeeper. For non-protected it is all 0s. |
Shikha Panwar | 0503cb0 | 2024-01-05 10:11:28 +0000 | [diff] [blame] | 101 | let mut skp_secret = Zeroizing::new([0u8; SECRET_SIZE]); |
| 102 | if super::is_strict_boot() { |
Shikha Panwar | e45e942 | 2024-02-28 21:18:10 +0000 | [diff] [blame^] | 103 | let sk_service = get_secretkeeper_service(vm_service)?; |
| 104 | let mut session = |
| 105 | SkSession::new(sk_service, &explicit_dice, Some(get_secretkeeper_identity()?))?; |
| 106 | let id = super::get_instance_id()?.ok_or(anyhow!("Missing instance_id"))?; |
Shikha Panwar | 14abe44 | 2024-02-23 15:04:27 +0000 | [diff] [blame] | 107 | let explicit_dice_chain = explicit_dice |
| 108 | .explicit_key_dice_chain() |
| 109 | .ok_or(anyhow!("Missing explicit dice chain, this is unusual"))?; |
Shikha Panwar | f18ffc3 | 2024-03-04 16:57:02 +0000 | [diff] [blame] | 110 | let policy = sealing_policy(explicit_dice_chain) |
| 111 | .map_err(|e| anyhow!("Failed to build a sealing_policy: {e}"))?; |
Shikha Panwar | 0503cb0 | 2024-01-05 10:11:28 +0000 | [diff] [blame] | 112 | if super::is_new_instance() { |
Shikha Panwar | 14abe44 | 2024-02-23 15:04:27 +0000 | [diff] [blame] | 113 | // New instance -> create a secret & store in Secretkeeper. |
Shikha Panwar | 0503cb0 | 2024-01-05 10:11:28 +0000 | [diff] [blame] | 114 | *skp_secret = rand::random(); |
Shikha Panwar | f18ffc3 | 2024-03-04 16:57:02 +0000 | [diff] [blame] | 115 | store_secret(&mut session, id, skp_secret.clone(), policy) |
| 116 | .context("Failed to store secret in Secretkeeper")?; |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 117 | } else { |
Shikha Panwar | 0503cb0 | 2024-01-05 10:11:28 +0000 | [diff] [blame] | 118 | // Subsequent run of the pVM -> get the secret stored in Secretkeeper. |
Shikha Panwar | f18ffc3 | 2024-03-04 16:57:02 +0000 | [diff] [blame] | 119 | *skp_secret = get_secret(&mut session, id, Some(policy)) |
| 120 | .context("Failed to get secret from Secretkeeper")?; |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 121 | } |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 122 | } |
Shikha Panwar | 0503cb0 | 2024-01-05 10:11:28 +0000 | [diff] [blame] | 123 | Ok(Self::V2 { |
| 124 | dice_artifacts: explicit_dice, |
| 125 | skp_secret: ZVec::try_from(skp_secret.to_vec())?, |
| 126 | }) |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 127 | } |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 128 | |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 129 | pub fn dice_artifacts(&self) -> &dyn DiceArtifacts { |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 130 | match self { |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 131 | Self::V2 { dice_artifacts, .. } => dice_artifacts, |
| 132 | Self::V1 { dice_artifacts } => dice_artifacts, |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | fn get_vm_secret(&self, salt: &[u8], identifier: &[u8], key: &mut [u8]) -> Result<()> { |
| 137 | match self { |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 138 | Self::V2 { dice_artifacts, skp_secret } => { |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 139 | let mut hasher = sha::Sha256::new(); |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 140 | hasher.update(dice_artifacts.cdi_seal()); |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 141 | hasher.update(skp_secret); |
| 142 | hkdf(key, Md::sha256(), &hasher.finish(), salt, identifier)? |
| 143 | } |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 144 | Self::V1 { dice_artifacts } => { |
| 145 | hkdf(key, Md::sha256(), dice_artifacts.cdi_seal(), salt, identifier)? |
| 146 | } |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 147 | } |
| 148 | Ok(()) |
| 149 | } |
| 150 | |
Shikha Panwar | 3d3a70a | 2023-08-21 20:02:08 +0000 | [diff] [blame] | 151 | /// Derive sealing key for payload with following identifier. |
| 152 | pub fn derive_payload_sealing_key(&self, identifier: &[u8], key: &mut [u8]) -> Result<()> { |
| 153 | self.get_vm_secret(SALT_PAYLOAD_SERVICE, identifier, key) |
| 154 | } |
| 155 | |
| 156 | /// Derive encryptedstore key. This uses hardcoded random salt & fixed identifier. |
| 157 | pub fn derive_encryptedstore_key(&self, key: &mut [u8]) -> Result<()> { |
| 158 | self.get_vm_secret(SALT_ENCRYPTED_STORE, ENCRYPTEDSTORE_KEY_IDENTIFIER.as_bytes(), key) |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 162 | // Construct a sealing policy on the dice chain. VMs uses the following set of constraint for |
| 163 | // protecting secrets against rollback of boot images. |
| 164 | // 1. ExactMatch on AUTHORITY_HASH (Required ie, each DiceChainEntry must have it). |
| 165 | // 2. ExactMatch on MODE (Required) - Secret should be inaccessible if any of the runtime |
| 166 | // configuration changes. For ex, the secrets stored with a boot stage being in Normal mode |
| 167 | // should be inaccessible when the same stage is booted in Debug mode. |
| 168 | // 3. GreaterOrEqual on SECURITY_VERSION (Optional): The secrets will be accessible if version of |
| 169 | // any image is greater or equal to the set version. This is an optional field, certain |
| 170 | // components may chose to prevent booting of rollback images for ex, ABL is expected to provide |
| 171 | // rollback protection of pvmfw. Such components may chose to not put SECURITY_VERSION in the |
| 172 | // corresponding DiceChainEntry. |
Shikha Panwar | 101ac8f | 2024-01-19 11:20:18 +0000 | [diff] [blame] | 173 | // 4. For each Subcomponent on the last DiceChainEntry (which corresponds to VM payload, See |
| 174 | // microdroid_manager/src/vm_config.cddl): |
| 175 | // - GreaterOrEqual on SECURITY_VERSION (Required) |
| 176 | // - ExactMatch on AUTHORITY_HASH (Required). |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 177 | fn sealing_policy(dice: &[u8]) -> Result<Vec<u8>, String> { |
| 178 | let constraint_spec = [ |
Shikha Panwar | 101ac8f | 2024-01-19 11:20:18 +0000 | [diff] [blame] | 179 | ConstraintSpec::new( |
| 180 | ConstraintType::ExactMatch, |
| 181 | vec![AUTHORITY_HASH], |
| 182 | MissingAction::Fail, |
| 183 | CertIndex::All, |
| 184 | ), |
| 185 | ConstraintSpec::new( |
| 186 | ConstraintType::ExactMatch, |
| 187 | vec![MODE], |
| 188 | MissingAction::Fail, |
| 189 | CertIndex::All, |
| 190 | ), |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 191 | ConstraintSpec::new( |
| 192 | ConstraintType::GreaterOrEqual, |
| 193 | vec![CONFIG_DESC, SECURITY_VERSION], |
Shikha Panwar | 091bfbe | 2024-01-15 09:37:06 +0000 | [diff] [blame] | 194 | MissingAction::Ignore, |
Shikha Panwar | 101ac8f | 2024-01-19 11:20:18 +0000 | [diff] [blame] | 195 | CertIndex::All, |
| 196 | ), |
| 197 | ConstraintSpec::new( |
| 198 | ConstraintType::GreaterOrEqual, |
| 199 | vec![ |
| 200 | CONFIG_DESC, |
| 201 | SUBCOMPONENT_DESCRIPTORS, |
| 202 | WILDCARD_FULL_ARRAY, |
| 203 | SUBCOMPONENT_SECURITY_VERSION, |
| 204 | ], |
| 205 | MissingAction::Fail, |
| 206 | CertIndex::FromEnd(PAYLOAD_INDEX_FROM_END), |
| 207 | ), |
| 208 | ConstraintSpec::new( |
| 209 | ConstraintType::ExactMatch, |
| 210 | vec![ |
| 211 | CONFIG_DESC, |
| 212 | SUBCOMPONENT_DESCRIPTORS, |
| 213 | WILDCARD_FULL_ARRAY, |
| 214 | SUBCOMPONENT_AUTHORITY_HASH, |
| 215 | ], |
| 216 | MissingAction::Fail, |
| 217 | CertIndex::FromEnd(PAYLOAD_INDEX_FROM_END), |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 218 | ), |
| 219 | ]; |
| 220 | |
Shikha Panwar | e763ce1 | 2024-01-19 11:20:18 +0000 | [diff] [blame] | 221 | policy_for_dice_chain(dice, &constraint_spec)? |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 222 | .to_vec() |
| 223 | .map_err(|e| format!("DicePolicy construction failed {e:?}")) |
| 224 | } |
| 225 | |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 226 | fn store_secret( |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 227 | session: &mut SkSession, |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 228 | id: [u8; ID_SIZE], |
| 229 | secret: Zeroizing<[u8; SECRET_SIZE]>, |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 230 | sealing_policy: Vec<u8>, |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 231 | ) -> Result<()> { |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 232 | let store_request = StoreSecretRequest { id: Id(id), secret: Secret(*secret), sealing_policy }; |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 233 | log::info!("Secretkeeper operation: {:?}", store_request); |
| 234 | |
| 235 | let store_request = store_request.serialize_to_packet().to_vec().map_err(anyhow_err)?; |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 236 | let store_response = session.secret_management_request(&store_request)?; |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 237 | let store_response = ResponsePacket::from_slice(&store_response).map_err(anyhow_err)?; |
| 238 | let response_type = store_response.response_type().map_err(anyhow_err)?; |
| 239 | ensure!( |
| 240 | response_type == ResponseType::Success, |
| 241 | "Secretkeeper store failed with error: {:?}", |
| 242 | *SecretkeeperError::deserialize_from_packet(store_response).map_err(anyhow_err)? |
| 243 | ); |
| 244 | Ok(()) |
| 245 | } |
| 246 | |
| 247 | fn get_secret( |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 248 | session: &mut SkSession, |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 249 | id: [u8; ID_SIZE], |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 250 | updated_sealing_policy: Option<Vec<u8>>, |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 251 | ) -> Result<[u8; SECRET_SIZE]> { |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 252 | let get_request = GetSecretRequest { id: Id(id), updated_sealing_policy }; |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 253 | log::info!("Secretkeeper operation: {:?}", get_request); |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 254 | let get_request = get_request.serialize_to_packet().to_vec().map_err(anyhow_err)?; |
Shikha Panwar | 6b17832 | 2023-12-23 00:05:17 +0000 | [diff] [blame] | 255 | let get_response = session.secret_management_request(&get_request)?; |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 256 | let get_response = ResponsePacket::from_slice(&get_response).map_err(anyhow_err)?; |
| 257 | let response_type = get_response.response_type().map_err(anyhow_err)?; |
| 258 | ensure!( |
| 259 | response_type == ResponseType::Success, |
| 260 | "Secretkeeper get failed with error: {:?}", |
| 261 | *SecretkeeperError::deserialize_from_packet(get_response).map_err(anyhow_err)? |
| 262 | ); |
| 263 | let get_response = |
| 264 | *GetSecretResponse::deserialize_from_packet(get_response).map_err(anyhow_err)?; |
| 265 | Ok(get_response.secret.0) |
| 266 | } |
| 267 | |
| 268 | #[inline] |
| 269 | fn anyhow_err<E: core::fmt::Debug>(err: E) -> anyhow::Error { |
| 270 | anyhow!("{:?}", err) |
| 271 | } |
| 272 | |
Shikha Panwar | e45e942 | 2024-02-28 21:18:10 +0000 | [diff] [blame^] | 273 | fn get_secretkeeper_service( |
Shikha Panwar | 5d6a675 | 2023-12-14 22:08:26 +0000 | [diff] [blame] | 274 | host: &Strong<dyn IVirtualMachineService>, |
Shikha Panwar | e45e942 | 2024-02-28 21:18:10 +0000 | [diff] [blame^] | 275 | ) -> Result<Strong<dyn ISecretkeeper>> { |
| 276 | Ok(host |
| 277 | .getSecretkeeper() |
| 278 | // TODO rename this error! |
| 279 | .map_err(|e| { |
| 280 | super::MicrodroidError::FailedToConnectToVirtualizationService(format!( |
| 281 | "Failed to get Secretkeeper: {e:?}" |
| 282 | )) |
| 283 | })?) |
Shikha Panwar | 95084df | 2023-07-22 11:47:45 +0000 | [diff] [blame] | 284 | } |