Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 1 | // Copyright 2021, 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 | //! Provide the [`KeyMintDevice`] wrapper for operating directly on a KeyMint device. |
| 16 | |
| 17 | use crate::{ |
| 18 | database::{ |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 19 | BlobInfo, BlobMetaData, BlobMetaEntry, CertificateInfo, DateTime, KeyEntry, |
| 20 | KeyEntryLoadBits, KeyIdGuard, KeyMetaData, KeyMetaEntry, KeyType, KeystoreDB, |
| 21 | SubComponentType, Uuid, |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 22 | }, |
Janis Danisevskis | 5c74821 | 2021-05-17 17:13:56 -0700 | [diff] [blame] | 23 | error::{map_km_error, Error, ErrorCode}, |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 24 | globals::get_keymint_device, |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 25 | ks_err, |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 26 | super_key::KeyBlob, |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 27 | utils::{key_characteristics_to_internal, watchdog as wd, AID_KEYSTORE}, |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 28 | }; |
| 29 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 30 | HardwareAuthToken::HardwareAuthToken, IKeyMintDevice::IKeyMintDevice, |
| 31 | IKeyMintOperation::IKeyMintOperation, KeyCharacteristics::KeyCharacteristics, |
| 32 | KeyCreationResult::KeyCreationResult, KeyParameter::KeyParameter, KeyPurpose::KeyPurpose, |
| 33 | SecurityLevel::SecurityLevel, |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 34 | }; |
| 35 | use android_system_keystore2::aidl::android::system::keystore2::{ |
Paul Crowley | 618869e | 2021-04-08 20:30:54 -0700 | [diff] [blame] | 36 | Domain::Domain, KeyDescriptor::KeyDescriptor, ResponseCode::ResponseCode, |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 37 | }; |
| 38 | use anyhow::{Context, Result}; |
| 39 | use binder::Strong; |
| 40 | |
| 41 | /// Wrapper for operating directly on a KeyMint device. |
| 42 | /// These methods often mirror methods in [`crate::security_level`]. However |
| 43 | /// the functions in [`crate::security_level`] make assumptions that hold, and has side effects |
| 44 | /// that make sense, only if called by an external client through binder. |
| 45 | /// In addition we are trying to maintain a separation between interface services |
| 46 | /// so that the architecture is compatible with a future move to multiple thread pools. |
| 47 | /// So the simplest approach today is to write new implementations of them for internal use. |
| 48 | /// Because these methods run very early, we don't even try to cooperate with |
| 49 | /// the operation slot database; we assume there will be plenty of slots. |
| 50 | pub struct KeyMintDevice { |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 51 | km_dev: Strong<dyn IKeyMintDevice>, |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 52 | km_uuid: Uuid, |
Janis Danisevskis | 5c74821 | 2021-05-17 17:13:56 -0700 | [diff] [blame] | 53 | version: i32, |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 54 | security_level: SecurityLevel, |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | impl KeyMintDevice { |
Janis Danisevskis | 5c74821 | 2021-05-17 17:13:56 -0700 | [diff] [blame] | 58 | /// Version number of KeyMasterDevice@V4_0 |
| 59 | pub const KEY_MASTER_V4_0: i32 = 40; |
| 60 | /// Version number of KeyMasterDevice@V4_1 |
| 61 | pub const KEY_MASTER_V4_1: i32 = 41; |
| 62 | /// Version number of KeyMintDevice@V1 |
| 63 | pub const KEY_MINT_V1: i32 = 100; |
David Drysdale | a6c82a9 | 2021-12-06 11:24:26 +0000 | [diff] [blame] | 64 | /// Version number of KeyMintDevice@V2 |
| 65 | pub const KEY_MINT_V2: i32 = 200; |
Eran Messeri | 637259c | 2022-10-31 12:23:36 +0000 | [diff] [blame] | 66 | /// Version number of KeyMintDevice@V3 |
| 67 | pub const KEY_MINT_V3: i32 = 300; |
Janis Danisevskis | 5c74821 | 2021-05-17 17:13:56 -0700 | [diff] [blame] | 68 | |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 69 | /// Get a [`KeyMintDevice`] for the given [`SecurityLevel`] |
| 70 | pub fn get(security_level: SecurityLevel) -> Result<KeyMintDevice> { |
Shaquille Johnson | aec2eca | 2022-11-30 17:08:05 +0000 | [diff] [blame] | 71 | let (km_dev, hw_info, km_uuid) = |
| 72 | get_keymint_device(&security_level).context(ks_err!("get_keymint_device failed"))?; |
Janis Danisevskis | 5c74821 | 2021-05-17 17:13:56 -0700 | [diff] [blame] | 73 | |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 74 | Ok(KeyMintDevice { |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 75 | km_dev, |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 76 | km_uuid, |
| 77 | version: hw_info.versionNumber, |
| 78 | security_level: hw_info.securityLevel, |
| 79 | }) |
Janis Danisevskis | 5c74821 | 2021-05-17 17:13:56 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /// Get a [`KeyMintDevice`] for the given [`SecurityLevel`], return |
| 83 | /// [`None`] if the error `HARDWARE_TYPE_UNAVAILABLE` is returned |
| 84 | pub fn get_or_none(security_level: SecurityLevel) -> Result<Option<KeyMintDevice>> { |
| 85 | KeyMintDevice::get(security_level).map(Some).or_else(|e| { |
| 86 | match e.root_cause().downcast_ref::<Error>() { |
| 87 | Some(Error::Km(ErrorCode::HARDWARE_TYPE_UNAVAILABLE)) => Ok(None), |
| 88 | _ => Err(e), |
| 89 | } |
| 90 | }) |
| 91 | } |
| 92 | |
| 93 | /// Returns the version of the underlying KeyMint/KeyMaster device. |
| 94 | pub fn version(&self) -> i32 { |
| 95 | self.version |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 98 | /// Returns the self advertised security level of the KeyMint device. |
| 99 | /// This may differ from the requested security level if the best security level |
| 100 | /// on the device is Software. |
| 101 | pub fn security_level(&self) -> SecurityLevel { |
| 102 | self.security_level |
| 103 | } |
| 104 | |
Paul Crowley | 618869e | 2021-04-08 20:30:54 -0700 | [diff] [blame] | 105 | /// Create a KM key and store in the database. |
| 106 | pub fn create_and_store_key<F>( |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 107 | &self, |
| 108 | db: &mut KeystoreDB, |
| 109 | key_desc: &KeyDescriptor, |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 110 | key_type: KeyType, |
Paul Crowley | 618869e | 2021-04-08 20:30:54 -0700 | [diff] [blame] | 111 | creator: F, |
| 112 | ) -> Result<()> |
| 113 | where |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 114 | F: FnOnce(&Strong<dyn IKeyMintDevice>) -> Result<KeyCreationResult, binder::Status>, |
Paul Crowley | 618869e | 2021-04-08 20:30:54 -0700 | [diff] [blame] | 115 | { |
Shaquille Johnson | aec2eca | 2022-11-30 17:08:05 +0000 | [diff] [blame] | 116 | let creation_result = |
| 117 | map_km_error(creator(&self.km_dev)).context(ks_err!("creator failed"))?; |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 118 | let key_parameters = key_characteristics_to_internal(creation_result.keyCharacteristics); |
| 119 | |
Shaquille Johnson | aec2eca | 2022-11-30 17:08:05 +0000 | [diff] [blame] | 120 | let creation_date = DateTime::now().context(ks_err!("DateTime::now() failed"))?; |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 121 | |
| 122 | let mut key_metadata = KeyMetaData::new(); |
| 123 | key_metadata.add(KeyMetaEntry::CreationDate(creation_date)); |
| 124 | let mut blob_metadata = BlobMetaData::new(); |
| 125 | blob_metadata.add(BlobMetaEntry::KmUuid(self.km_uuid)); |
| 126 | |
| 127 | db.store_new_key( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 128 | key_desc, |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 129 | key_type, |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 130 | &key_parameters, |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 131 | &BlobInfo::new(&creation_result.keyBlob, &blob_metadata), |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 132 | &CertificateInfo::new(None, None), |
| 133 | &key_metadata, |
| 134 | &self.km_uuid, |
| 135 | ) |
Shaquille Johnson | aec2eca | 2022-11-30 17:08:05 +0000 | [diff] [blame] | 136 | .context(ks_err!("store_new_key failed"))?; |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 137 | Ok(()) |
| 138 | } |
| 139 | |
Paul Crowley | 618869e | 2021-04-08 20:30:54 -0700 | [diff] [blame] | 140 | /// Generate a KeyDescriptor for internal-use keys. |
| 141 | pub fn internal_descriptor(alias: String) -> KeyDescriptor { |
| 142 | KeyDescriptor { |
| 143 | domain: Domain::APP, |
| 144 | nspace: AID_KEYSTORE as i64, |
| 145 | alias: Some(alias), |
| 146 | blob: None, |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /// Look up an internal-use key in the database given a key descriptor. |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 151 | fn lookup_from_desc( |
Paul Crowley | 618869e | 2021-04-08 20:30:54 -0700 | [diff] [blame] | 152 | db: &mut KeystoreDB, |
| 153 | key_desc: &KeyDescriptor, |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 154 | key_type: KeyType, |
Paul Crowley | 618869e | 2021-04-08 20:30:54 -0700 | [diff] [blame] | 155 | ) -> Result<(KeyIdGuard, KeyEntry)> { |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 156 | db.load_key_entry(key_desc, key_type, KeyEntryLoadBits::KM, AID_KEYSTORE, |_, _| Ok(())) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 157 | .context(ks_err!("load_key_entry failed.")) |
Paul Crowley | 618869e | 2021-04-08 20:30:54 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /// Look up the key in the database, and return None if it is absent. |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 161 | fn not_found_is_none( |
Paul Crowley | 618869e | 2021-04-08 20:30:54 -0700 | [diff] [blame] | 162 | lookup: Result<(KeyIdGuard, KeyEntry)>, |
| 163 | ) -> Result<Option<(KeyIdGuard, KeyEntry)>> { |
| 164 | match lookup { |
| 165 | Ok(result) => Ok(Some(result)), |
| 166 | Err(e) => match e.root_cause().downcast_ref::<Error>() { |
| 167 | Some(&Error::Rc(ResponseCode::KEY_NOT_FOUND)) => Ok(None), |
| 168 | _ => Err(e), |
| 169 | }, |
| 170 | } |
| 171 | } |
| 172 | |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 173 | /// This does the lookup and store in separate transactions; caller must |
| 174 | /// hold a lock before calling. |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 175 | pub fn lookup_or_generate_key<F>( |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 176 | &self, |
| 177 | db: &mut KeystoreDB, |
| 178 | key_desc: &KeyDescriptor, |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 179 | key_type: KeyType, |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 180 | params: &[KeyParameter], |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 181 | validate_characteristics: F, |
| 182 | ) -> Result<(KeyIdGuard, KeyBlob)> |
| 183 | where |
| 184 | F: FnOnce(&[KeyCharacteristics]) -> bool, |
| 185 | { |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 186 | // We use a separate transaction for the lookup than for the store |
| 187 | // - to keep the code simple |
| 188 | // - because the caller needs to hold a lock in any case |
| 189 | // - because it avoids holding database locks during slow |
| 190 | // KeyMint operations |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 191 | let lookup = Self::not_found_is_none(Self::lookup_from_desc(db, key_desc, key_type)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 192 | .context(ks_err!("first lookup failed"))?; |
Janis Danisevskis | 5c74821 | 2021-05-17 17:13:56 -0700 | [diff] [blame] | 193 | |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 194 | if let Some((key_id_guard, mut key_entry)) = lookup { |
Janis Danisevskis | 5c74821 | 2021-05-17 17:13:56 -0700 | [diff] [blame] | 195 | // If the key is associated with a different km instance |
| 196 | // or if there is no blob metadata for some reason the key entry |
| 197 | // is considered corrupted and needs to be replaced with a new one. |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 198 | let key_blob = key_entry.take_key_blob_info().and_then(|(key_blob, blob_metadata)| { |
| 199 | if Some(&self.km_uuid) == blob_metadata.km_uuid() { |
| 200 | Some(key_blob) |
| 201 | } else { |
| 202 | None |
| 203 | } |
| 204 | }); |
| 205 | |
| 206 | if let Some(key_blob_vec) = key_blob { |
| 207 | let (key_characteristics, key_blob) = self |
| 208 | .upgrade_keyblob_if_required_with( |
| 209 | db, |
| 210 | &key_id_guard, |
| 211 | KeyBlob::NonSensitive(key_blob_vec), |
| 212 | |key_blob| { |
| 213 | map_km_error({ |
| 214 | let _wp = wd::watch_millis( |
| 215 | concat!( |
| 216 | "In KeyMintDevice::lookup_or_generate_key: ", |
| 217 | "calling getKeyCharacteristics." |
| 218 | ), |
| 219 | 500, |
| 220 | ); |
| 221 | self.km_dev.getKeyCharacteristics(key_blob, &[], &[]) |
| 222 | }) |
| 223 | }, |
| 224 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 225 | .context(ks_err!("calling getKeyCharacteristics"))?; |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 226 | |
| 227 | if validate_characteristics(&key_characteristics) { |
| 228 | return Ok((key_id_guard, key_blob)); |
| 229 | } |
| 230 | |
| 231 | // If this point is reached the existing key is considered outdated or corrupted |
| 232 | // in some way. It will be replaced with a new key below. |
| 233 | }; |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 234 | } |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 235 | |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 236 | self.create_and_store_key(db, key_desc, key_type, |km_dev| { |
| 237 | km_dev.generateKey(params, None) |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 238 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 239 | .context(ks_err!("generate_and_store_key failed"))?; |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 240 | Self::lookup_from_desc(db, key_desc, key_type) |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 241 | .and_then(|(key_id_guard, mut key_entry)| { |
| 242 | Ok(( |
| 243 | key_id_guard, |
| 244 | key_entry |
| 245 | .take_key_blob_info() |
| 246 | .ok_or(Error::Rc(ResponseCode::KEY_NOT_FOUND)) |
| 247 | .map(|(key_blob, _)| KeyBlob::NonSensitive(key_blob)) |
Shaquille Johnson | aec2eca | 2022-11-30 17:08:05 +0000 | [diff] [blame] | 248 | .context(ks_err!("Missing key blob info."))?, |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 249 | )) |
| 250 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 251 | .context(ks_err!("second lookup failed")) |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /// Call the passed closure; if it returns `KEY_REQUIRES_UPGRADE`, call upgradeKey, and |
| 255 | /// write the upgraded key to the database. |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 256 | fn upgrade_keyblob_if_required_with<'a, T, F>( |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 257 | &self, |
| 258 | db: &mut KeystoreDB, |
Paul Crowley | 618869e | 2021-04-08 20:30:54 -0700 | [diff] [blame] | 259 | key_id_guard: &KeyIdGuard, |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 260 | key_blob: KeyBlob<'a>, |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 261 | f: F, |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 262 | ) -> Result<(T, KeyBlob<'a>)> |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 263 | where |
| 264 | F: Fn(&[u8]) -> Result<T, Error>, |
| 265 | { |
David Drysdale | 96db425 | 2023-07-05 16:55:00 +0100 | [diff] [blame] | 266 | let (f_result, upgraded_blob) = crate::utils::upgrade_keyblob_if_required_with( |
| 267 | &*self.km_dev, |
David Drysdale | 5accbaa | 2023-04-12 18:47:10 +0100 | [diff] [blame] | 268 | self.version(), |
David Drysdale | 96db425 | 2023-07-05 16:55:00 +0100 | [diff] [blame] | 269 | &key_blob, |
| 270 | &[], |
| 271 | f, |
| 272 | |upgraded_blob| { |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 273 | let mut new_blob_metadata = BlobMetaData::new(); |
| 274 | new_blob_metadata.add(BlobMetaEntry::KmUuid(self.km_uuid)); |
| 275 | |
| 276 | db.set_blob( |
Paul Crowley | 618869e | 2021-04-08 20:30:54 -0700 | [diff] [blame] | 277 | key_id_guard, |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 278 | SubComponentType::KEY_BLOB, |
David Drysdale | 96db425 | 2023-07-05 16:55:00 +0100 | [diff] [blame] | 279 | Some(upgraded_blob), |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 280 | Some(&new_blob_metadata), |
| 281 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 282 | .context(ks_err!("Failed to insert upgraded blob into the database"))?; |
David Drysdale | 96db425 | 2023-07-05 16:55:00 +0100 | [diff] [blame] | 283 | Ok(()) |
| 284 | }, |
| 285 | )?; |
| 286 | let returned_blob = match upgraded_blob { |
| 287 | None => key_blob, |
| 288 | Some(upgraded_blob) => KeyBlob::NonSensitive(upgraded_blob), |
| 289 | }; |
| 290 | Ok((f_result, returned_blob)) |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /// Use the created key in an operation that can be done with |
| 294 | /// a call to begin followed by a call to finish. |
Paul Crowley | 618869e | 2021-04-08 20:30:54 -0700 | [diff] [blame] | 295 | #[allow(clippy::too_many_arguments)] |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 296 | pub fn use_key_in_one_step( |
| 297 | &self, |
| 298 | db: &mut KeystoreDB, |
Paul Crowley | 618869e | 2021-04-08 20:30:54 -0700 | [diff] [blame] | 299 | key_id_guard: &KeyIdGuard, |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 300 | key_blob: &[u8], |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 301 | purpose: KeyPurpose, |
| 302 | operation_parameters: &[KeyParameter], |
Paul Crowley | 618869e | 2021-04-08 20:30:54 -0700 | [diff] [blame] | 303 | auth_token: Option<&HardwareAuthToken>, |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 304 | input: &[u8], |
| 305 | ) -> Result<Vec<u8>> { |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 306 | let key_blob = KeyBlob::Ref(key_blob); |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 307 | |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 308 | let (begin_result, _) = self |
| 309 | .upgrade_keyblob_if_required_with(db, key_id_guard, key_blob, |blob| { |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 310 | map_km_error({ |
| 311 | let _wp = wd::watch_millis("In use_key_in_one_step: calling: begin", 500); |
Janis Danisevskis | acebfa2 | 2021-05-25 10:56:10 -0700 | [diff] [blame] | 312 | self.km_dev.begin(purpose, blob, operation_parameters, auth_token) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 313 | }) |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 314 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 315 | .context(ks_err!("Failed to begin operation."))?; |
| 316 | let operation: Strong<dyn IKeyMintOperation> = |
| 317 | begin_result.operation.ok_or_else(Error::sys).context(ks_err!("Operation missing"))?; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 318 | map_km_error({ |
| 319 | let _wp = wd::watch_millis("In use_key_in_one_step: calling: finish", 500); |
| 320 | operation.finish(Some(input), None, None, None, None) |
| 321 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 322 | .context(ks_err!("Failed to finish operation.")) |
Paul Crowley | ef611e5 | 2021-04-20 14:43:04 -0700 | [diff] [blame] | 323 | } |
| 324 | } |