Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 1 | // Copyright 2020, 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 implements utility functions used by the Keystore 2.0 service |
| 16 | //! implementation. |
| 17 | |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 18 | use crate::error::{map_binder_status, map_km_error, Error, ErrorCode}; |
| 19 | use crate::key_parameter::KeyParameter; |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 20 | use crate::permission; |
| 21 | use crate::permission::{KeyPerm, KeyPermSet, KeystorePerm}; |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 22 | use crate::{ |
| 23 | database::{KeyType, KeystoreDB}, |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 24 | globals::LEGACY_IMPORTER, |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 25 | }; |
Shawn Willden | 708744a | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 26 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 27 | IKeyMintDevice::IKeyMintDevice, KeyCharacteristics::KeyCharacteristics, |
| 28 | KeyParameter::KeyParameter as KmKeyParameter, Tag::Tag, |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 29 | }; |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 30 | use android_os_permissions_aidl::aidl::android::os::IPermissionController; |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 31 | use android_security_apc::aidl::android::security::apc::{ |
| 32 | IProtectedConfirmation::{FLAG_UI_OPTION_INVERTED, FLAG_UI_OPTION_MAGNIFIED}, |
| 33 | ResponseCode::ResponseCode as ApcResponseCode, |
| 34 | }; |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 35 | use android_system_keystore2::aidl::android::system::keystore2::{ |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 36 | Authorization::Authorization, Domain::Domain, KeyDescriptor::KeyDescriptor, |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 37 | }; |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 38 | use anyhow::{Context, Result}; |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 39 | use binder::{Strong, ThreadState}; |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 40 | use keystore2_apc_compat::{ |
| 41 | ApcCompatUiOptions, APC_COMPAT_ERROR_ABORTED, APC_COMPAT_ERROR_CANCELLED, |
| 42 | APC_COMPAT_ERROR_IGNORED, APC_COMPAT_ERROR_OK, APC_COMPAT_ERROR_OPERATION_PENDING, |
| 43 | APC_COMPAT_ERROR_SYSTEM_ERROR, |
| 44 | }; |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 45 | use keystore2_crypto::{aes_gcm_decrypt, aes_gcm_encrypt, ZVec}; |
| 46 | use std::iter::IntoIterator; |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 47 | |
| 48 | /// This function uses its namesake in the permission module and in |
| 49 | /// combination with with_calling_sid from the binder crate to check |
| 50 | /// if the caller has the given keystore permission. |
| 51 | pub fn check_keystore_permission(perm: KeystorePerm) -> anyhow::Result<()> { |
| 52 | ThreadState::with_calling_sid(|calling_sid| { |
| 53 | permission::check_keystore_permission( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 54 | calling_sid.ok_or_else(Error::sys).context( |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 55 | "In check_keystore_permission: Cannot check permission without calling_sid.", |
| 56 | )?, |
| 57 | perm, |
| 58 | ) |
| 59 | }) |
| 60 | } |
| 61 | |
| 62 | /// This function uses its namesake in the permission module and in |
| 63 | /// combination with with_calling_sid from the binder crate to check |
| 64 | /// if the caller has the given grant permission. |
| 65 | pub fn check_grant_permission(access_vec: KeyPermSet, key: &KeyDescriptor) -> anyhow::Result<()> { |
| 66 | ThreadState::with_calling_sid(|calling_sid| { |
| 67 | permission::check_grant_permission( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 68 | calling_sid.ok_or_else(Error::sys).context( |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 69 | "In check_grant_permission: Cannot check permission without calling_sid.", |
| 70 | )?, |
| 71 | access_vec, |
| 72 | key, |
| 73 | ) |
| 74 | }) |
| 75 | } |
| 76 | |
| 77 | /// This function uses its namesake in the permission module and in |
| 78 | /// combination with with_calling_sid from the binder crate to check |
| 79 | /// if the caller has the given key permission. |
| 80 | pub fn check_key_permission( |
| 81 | perm: KeyPerm, |
| 82 | key: &KeyDescriptor, |
| 83 | access_vector: &Option<KeyPermSet>, |
| 84 | ) -> anyhow::Result<()> { |
| 85 | ThreadState::with_calling_sid(|calling_sid| { |
| 86 | permission::check_key_permission( |
Janis Danisevskis | 4576002 | 2021-01-19 16:34:10 -0800 | [diff] [blame] | 87 | ThreadState::get_calling_uid(), |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 88 | calling_sid |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 89 | .ok_or_else(Error::sys) |
| 90 | .context("In check_key_permission: Cannot check permission without calling_sid.")?, |
| 91 | perm, |
| 92 | key, |
| 93 | access_vector, |
| 94 | ) |
| 95 | }) |
| 96 | } |
| 97 | |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 98 | /// This function checks whether a given tag corresponds to the access of device identifiers. |
| 99 | pub fn is_device_id_attestation_tag(tag: Tag) -> bool { |
Janis Danisevskis | 83116e5 | 2021-04-06 13:36:58 -0700 | [diff] [blame] | 100 | matches!( |
| 101 | tag, |
| 102 | Tag::ATTESTATION_ID_IMEI |
| 103 | | Tag::ATTESTATION_ID_MEID |
| 104 | | Tag::ATTESTATION_ID_SERIAL |
| 105 | | Tag::DEVICE_UNIQUE_ATTESTATION |
| 106 | ) |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /// This function checks whether the calling app has the Android permissions needed to attest device |
Seth Moore | 66d9e90 | 2022-03-16 17:20:31 -0700 | [diff] [blame^] | 110 | /// identifiers. It throws an error if the permissions cannot be verified or if the caller doesn't |
| 111 | /// have the right permissions. Otherwise it returns silently. |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 112 | pub fn check_device_attestation_permissions() -> anyhow::Result<()> { |
Seth Moore | 66d9e90 | 2022-03-16 17:20:31 -0700 | [diff] [blame^] | 113 | check_android_permission("android.permission.READ_PRIVILEGED_PHONE_STATE") |
| 114 | } |
| 115 | |
| 116 | /// This function checks whether the calling app has the Android permissions needed to attest the |
| 117 | /// device-unique identifier. It throws an error if the permissions cannot be verified or if the |
| 118 | /// caller doesn't have the right permissions. Otherwise it returns silently. |
| 119 | pub fn check_unique_id_attestation_permissions() -> anyhow::Result<()> { |
| 120 | check_android_permission("android.permission.REQUEST_UNIQUE_ID_ATTESTATION") |
| 121 | } |
| 122 | |
| 123 | fn check_android_permission(permission: &str) -> anyhow::Result<()> { |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 124 | let permission_controller: Strong<dyn IPermissionController::IPermissionController> = |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 125 | binder::get_interface("permission")?; |
| 126 | |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 127 | let binder_result = { |
| 128 | let _wp = watchdog::watch_millis( |
| 129 | "In check_device_attestation_permissions: calling checkPermission.", |
| 130 | 500, |
| 131 | ); |
| 132 | permission_controller.checkPermission( |
Seth Moore | 66d9e90 | 2022-03-16 17:20:31 -0700 | [diff] [blame^] | 133 | permission, |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 134 | ThreadState::get_calling_pid(), |
| 135 | ThreadState::get_calling_uid() as i32, |
| 136 | ) |
| 137 | }; |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 138 | let has_permissions = map_binder_status(binder_result) |
| 139 | .context("In check_device_attestation_permissions: checkPermission failed")?; |
| 140 | match has_permissions { |
| 141 | true => Ok(()), |
| 142 | false => Err(Error::Km(ErrorCode::CANNOT_ATTEST_IDS)).context(concat!( |
| 143 | "In check_device_attestation_permissions: ", |
| 144 | "caller does not have the permission to attest device IDs" |
| 145 | )), |
| 146 | } |
| 147 | } |
| 148 | |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 149 | /// Converts a set of key characteristics as returned from KeyMint into the internal |
| 150 | /// representation of the keystore service. |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 151 | pub fn key_characteristics_to_internal( |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 152 | key_characteristics: Vec<KeyCharacteristics>, |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 153 | ) -> Vec<KeyParameter> { |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 154 | key_characteristics |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 155 | .into_iter() |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 156 | .flat_map(|aidl_key_char| { |
| 157 | let sec_level = aidl_key_char.securityLevel; |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 158 | aidl_key_char |
| 159 | .authorizations |
| 160 | .into_iter() |
| 161 | .map(move |aidl_kp| KeyParameter::new(aidl_kp.into(), sec_level)) |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 162 | }) |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 163 | .collect() |
| 164 | } |
| 165 | |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 166 | /// This function can be used to upgrade key blobs on demand. The return value of |
| 167 | /// `km_op` is inspected and if ErrorCode::KEY_REQUIRES_UPGRADE is encountered, |
| 168 | /// an attempt is made to upgrade the key blob. On success `new_blob_handler` is called |
| 169 | /// with the upgraded blob as argument. Then `km_op` is called a second time with the |
| 170 | /// upgraded blob as argument. On success a tuple of the `km_op`s result and the |
| 171 | /// optional upgraded blob is returned. |
| 172 | pub fn upgrade_keyblob_if_required_with<T, KmOp, NewBlobHandler>( |
| 173 | km_dev: &dyn IKeyMintDevice, |
| 174 | key_blob: &[u8], |
| 175 | upgrade_params: &[KmKeyParameter], |
| 176 | km_op: KmOp, |
| 177 | new_blob_handler: NewBlobHandler, |
| 178 | ) -> Result<(T, Option<Vec<u8>>)> |
| 179 | where |
| 180 | KmOp: Fn(&[u8]) -> Result<T, Error>, |
| 181 | NewBlobHandler: FnOnce(&[u8]) -> Result<()>, |
| 182 | { |
| 183 | match km_op(key_blob) { |
| 184 | Err(Error::Km(ErrorCode::KEY_REQUIRES_UPGRADE)) => { |
| 185 | let upgraded_blob = { |
| 186 | let _wp = watchdog::watch_millis( |
| 187 | "In utils::upgrade_keyblob_if_required_with: calling upgradeKey.", |
| 188 | 500, |
| 189 | ); |
| 190 | map_km_error(km_dev.upgradeKey(key_blob, upgrade_params)) |
| 191 | } |
| 192 | .context("In utils::upgrade_keyblob_if_required_with: Upgrade failed.")?; |
| 193 | |
| 194 | new_blob_handler(&upgraded_blob) |
| 195 | .context("In utils::upgrade_keyblob_if_required_with: calling new_blob_handler.")?; |
| 196 | |
| 197 | km_op(&upgraded_blob) |
| 198 | .map(|v| (v, Some(upgraded_blob))) |
| 199 | .context("In utils::upgrade_keyblob_if_required_with: Calling km_op after upgrade.") |
| 200 | } |
| 201 | r => r |
| 202 | .map(|v| (v, None)) |
| 203 | .context("In utils::upgrade_keyblob_if_required_with: Calling km_op."), |
| 204 | } |
| 205 | } |
| 206 | |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 207 | /// Converts a set of key characteristics from the internal representation into a set of |
| 208 | /// Authorizations as they are used to convey key characteristics to the clients of keystore. |
| 209 | pub fn key_parameters_to_authorizations( |
| 210 | parameters: Vec<crate::key_parameter::KeyParameter>, |
| 211 | ) -> Vec<Authorization> { |
| 212 | parameters.into_iter().map(|p| p.into_authorization()).collect() |
| 213 | } |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 214 | |
Hasini Gunasinghe | 66a2460 | 2021-05-12 19:03:12 +0000 | [diff] [blame] | 215 | /// This returns the current time (in milliseconds) as an instance of a monotonic clock, |
| 216 | /// by invoking the system call since Rust does not support getting monotonic time instance |
| 217 | /// as an integer. |
| 218 | pub fn get_current_time_in_milliseconds() -> i64 { |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 219 | let mut current_time = libc::timespec { tv_sec: 0, tv_nsec: 0 }; |
| 220 | // Following unsafe block includes one system call to get monotonic time. |
| 221 | // Therefore, it is not considered harmful. |
| 222 | unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC_RAW, &mut current_time) }; |
Hasini Gunasinghe | 66a2460 | 2021-05-12 19:03:12 +0000 | [diff] [blame] | 223 | current_time.tv_sec as i64 * 1000 + (current_time.tv_nsec as i64 / 1_000_000) |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 224 | } |
Janis Danisevskis | cd1fb3a | 2020-12-01 09:20:09 -0800 | [diff] [blame] | 225 | |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 226 | /// Converts a response code as returned by the Android Protected Confirmation HIDL compatibility |
| 227 | /// module (keystore2_apc_compat) into a ResponseCode as defined by the APC AIDL |
| 228 | /// (android.security.apc) spec. |
| 229 | pub fn compat_2_response_code(rc: u32) -> ApcResponseCode { |
| 230 | match rc { |
| 231 | APC_COMPAT_ERROR_OK => ApcResponseCode::OK, |
| 232 | APC_COMPAT_ERROR_CANCELLED => ApcResponseCode::CANCELLED, |
| 233 | APC_COMPAT_ERROR_ABORTED => ApcResponseCode::ABORTED, |
| 234 | APC_COMPAT_ERROR_OPERATION_PENDING => ApcResponseCode::OPERATION_PENDING, |
| 235 | APC_COMPAT_ERROR_IGNORED => ApcResponseCode::IGNORED, |
| 236 | APC_COMPAT_ERROR_SYSTEM_ERROR => ApcResponseCode::SYSTEM_ERROR, |
| 237 | _ => ApcResponseCode::SYSTEM_ERROR, |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /// Converts the UI Options flags as defined by the APC AIDL (android.security.apc) spec into |
| 242 | /// UI Options flags as defined by the Android Protected Confirmation HIDL compatibility |
| 243 | /// module (keystore2_apc_compat). |
| 244 | pub fn ui_opts_2_compat(opt: i32) -> ApcCompatUiOptions { |
| 245 | ApcCompatUiOptions { |
| 246 | inverted: (opt & FLAG_UI_OPTION_INVERTED) != 0, |
| 247 | magnified: (opt & FLAG_UI_OPTION_MAGNIFIED) != 0, |
| 248 | } |
| 249 | } |
| 250 | |
Janis Danisevskis | cd1fb3a | 2020-12-01 09:20:09 -0800 | [diff] [blame] | 251 | /// AID offset for uid space partitioning. |
Joel Galenson | 81a50f2 | 2021-07-29 15:39:10 -0700 | [diff] [blame] | 252 | pub const AID_USER_OFFSET: u32 = rustutils::users::AID_USER_OFFSET; |
Janis Danisevskis | cd1fb3a | 2020-12-01 09:20:09 -0800 | [diff] [blame] | 253 | |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 254 | /// AID of the keystore process itself, used for keys that |
| 255 | /// keystore generates for its own use. |
Joel Galenson | 81a50f2 | 2021-07-29 15:39:10 -0700 | [diff] [blame] | 256 | pub const AID_KEYSTORE: u32 = rustutils::users::AID_KEYSTORE; |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 257 | |
Janis Danisevskis | cd1fb3a | 2020-12-01 09:20:09 -0800 | [diff] [blame] | 258 | /// Extracts the android user from the given uid. |
| 259 | pub fn uid_to_android_user(uid: u32) -> u32 { |
Joel Galenson | 81a50f2 | 2021-07-29 15:39:10 -0700 | [diff] [blame] | 260 | rustutils::users::multiuser_get_user_id(uid) |
Janis Danisevskis | cd1fb3a | 2020-12-01 09:20:09 -0800 | [diff] [blame] | 261 | } |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 262 | |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 263 | /// List all key aliases for a given domain + namespace. |
| 264 | pub fn list_key_entries( |
| 265 | db: &mut KeystoreDB, |
| 266 | domain: Domain, |
| 267 | namespace: i64, |
| 268 | ) -> Result<Vec<KeyDescriptor>> { |
| 269 | let mut result = Vec::new(); |
| 270 | result.append( |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 271 | &mut LEGACY_IMPORTER |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 272 | .list_uid(domain, namespace) |
| 273 | .context("In list_key_entries: Trying to list legacy keys.")?, |
| 274 | ); |
| 275 | result.append( |
| 276 | &mut db |
| 277 | .list(domain, namespace, KeyType::Client) |
| 278 | .context("In list_key_entries: Trying to list keystore database.")?, |
| 279 | ); |
| 280 | result.sort_unstable(); |
| 281 | result.dedup(); |
| 282 | Ok(result) |
| 283 | } |
| 284 | |
Janis Danisevskis | 3d5a214 | 2021-05-05 07:31:24 -0700 | [diff] [blame] | 285 | /// This module provides helpers for simplified use of the watchdog module. |
| 286 | #[cfg(feature = "watchdog")] |
| 287 | pub mod watchdog { |
| 288 | pub use crate::watchdog::WatchPoint; |
| 289 | use crate::watchdog::Watchdog; |
| 290 | use lazy_static::lazy_static; |
| 291 | use std::sync::Arc; |
| 292 | use std::time::Duration; |
| 293 | |
| 294 | lazy_static! { |
| 295 | /// A Watchdog thread, that can be used to create watch points. |
| 296 | static ref WD: Arc<Watchdog> = Watchdog::new(Duration::from_secs(10)); |
| 297 | } |
| 298 | |
| 299 | /// Sets a watch point with `id` and a timeout of `millis` milliseconds. |
| 300 | pub fn watch_millis(id: &'static str, millis: u64) -> Option<WatchPoint> { |
| 301 | Watchdog::watch(&WD, id, Duration::from_millis(millis)) |
| 302 | } |
| 303 | |
| 304 | /// Like `watch_millis` but with a callback that is called every time a report |
| 305 | /// is printed about this watch point. |
| 306 | pub fn watch_millis_with( |
| 307 | id: &'static str, |
| 308 | millis: u64, |
| 309 | callback: impl Fn() -> String + Send + 'static, |
| 310 | ) -> Option<WatchPoint> { |
| 311 | Watchdog::watch_with(&WD, id, Duration::from_millis(millis), callback) |
| 312 | } |
| 313 | } |
| 314 | |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 315 | /// Trait implemented by objects that can be used to decrypt cipher text using AES-GCM. |
| 316 | pub trait AesGcm { |
| 317 | /// Deciphers `data` using the initialization vector `iv` and AEAD tag `tag` |
| 318 | /// and AES-GCM. The implementation provides the key material and selects |
| 319 | /// the implementation variant, e.g., AES128 or AES265. |
| 320 | fn decrypt(&self, data: &[u8], iv: &[u8], tag: &[u8]) -> Result<ZVec>; |
| 321 | |
| 322 | /// Encrypts `data` and returns the ciphertext, the initialization vector `iv` |
| 323 | /// and AEAD tag `tag`. The implementation provides the key material and selects |
| 324 | /// the implementation variant, e.g., AES128 or AES265. |
| 325 | fn encrypt(&self, plaintext: &[u8]) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>)>; |
| 326 | } |
| 327 | |
| 328 | /// Marks an object as AES-GCM key. |
| 329 | pub trait AesGcmKey { |
| 330 | /// Provides access to the raw key material. |
| 331 | fn key(&self) -> &[u8]; |
| 332 | } |
| 333 | |
| 334 | impl<T: AesGcmKey> AesGcm for T { |
| 335 | fn decrypt(&self, data: &[u8], iv: &[u8], tag: &[u8]) -> Result<ZVec> { |
| 336 | aes_gcm_decrypt(data, iv, tag, self.key()) |
| 337 | .context("In AesGcm<T>::decrypt: Decryption failed") |
| 338 | } |
| 339 | |
| 340 | fn encrypt(&self, plaintext: &[u8]) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>)> { |
| 341 | aes_gcm_encrypt(plaintext, self.key()).context("In AesGcm<T>::encrypt: Encryption failed.") |
| 342 | } |
| 343 | } |
| 344 | |
Janis Danisevskis | 3d5a214 | 2021-05-05 07:31:24 -0700 | [diff] [blame] | 345 | /// This module provides empty/noop implementations of the watch dog utility functions. |
| 346 | #[cfg(not(feature = "watchdog"))] |
| 347 | pub mod watchdog { |
| 348 | /// Noop watch point. |
| 349 | pub struct WatchPoint(); |
| 350 | /// Sets a Noop watch point. |
| 351 | fn watch_millis(_: &'static str, _: u64) -> Option<WatchPoint> { |
| 352 | None |
| 353 | } |
| 354 | |
| 355 | pub fn watch_millis_with( |
| 356 | _: &'static str, |
| 357 | _: u64, |
| 358 | _: impl Fn() -> String + Send + 'static, |
| 359 | ) -> Option<WatchPoint> { |
| 360 | None |
| 361 | } |
| 362 | } |
| 363 | |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 364 | #[cfg(test)] |
| 365 | mod tests { |
| 366 | use super::*; |
| 367 | use anyhow::Result; |
| 368 | |
| 369 | #[test] |
| 370 | fn check_device_attestation_permissions_test() -> Result<()> { |
| 371 | check_device_attestation_permissions().or_else(|error| { |
| 372 | match error.root_cause().downcast_ref::<Error>() { |
| 373 | // Expected: the context for this test might not be allowed to attest device IDs. |
| 374 | Some(Error::Km(ErrorCode::CANNOT_ATTEST_IDS)) => Ok(()), |
| 375 | // Other errors are unexpected |
| 376 | _ => Err(error), |
| 377 | } |
| 378 | }) |
| 379 | } |
| 380 | } |