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 | |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 18 | use crate::error::{map_binder_status, Error, ErrorCode}; |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 19 | use crate::permission; |
| 20 | use crate::permission::{KeyPerm, KeyPermSet, KeystorePerm}; |
Shawn Willden | 708744a | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 21 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 22 | KeyCharacteristics::KeyCharacteristics, Tag::Tag, |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 23 | }; |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 24 | use android_os_permissions_aidl::aidl::android::os::IPermissionController; |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 25 | use android_security_apc::aidl::android::security::apc::{ |
| 26 | IProtectedConfirmation::{FLAG_UI_OPTION_INVERTED, FLAG_UI_OPTION_MAGNIFIED}, |
| 27 | ResponseCode::ResponseCode as ApcResponseCode, |
| 28 | }; |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 29 | use android_system_keystore2::aidl::android::system::keystore2::{ |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 30 | Authorization::Authorization, KeyDescriptor::KeyDescriptor, |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 31 | }; |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 32 | use anyhow::Context; |
| 33 | use binder::{Strong, ThreadState}; |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 34 | use keystore2_apc_compat::{ |
| 35 | ApcCompatUiOptions, APC_COMPAT_ERROR_ABORTED, APC_COMPAT_ERROR_CANCELLED, |
| 36 | APC_COMPAT_ERROR_IGNORED, APC_COMPAT_ERROR_OK, APC_COMPAT_ERROR_OPERATION_PENDING, |
| 37 | APC_COMPAT_ERROR_SYSTEM_ERROR, |
| 38 | }; |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 39 | |
| 40 | /// This function uses its namesake in the permission module and in |
| 41 | /// combination with with_calling_sid from the binder crate to check |
| 42 | /// if the caller has the given keystore permission. |
| 43 | pub fn check_keystore_permission(perm: KeystorePerm) -> anyhow::Result<()> { |
| 44 | ThreadState::with_calling_sid(|calling_sid| { |
| 45 | permission::check_keystore_permission( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 46 | calling_sid.ok_or_else(Error::sys).context( |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 47 | "In check_keystore_permission: Cannot check permission without calling_sid.", |
| 48 | )?, |
| 49 | perm, |
| 50 | ) |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | /// This function uses its namesake in the permission module and in |
| 55 | /// combination with with_calling_sid from the binder crate to check |
| 56 | /// if the caller has the given grant permission. |
| 57 | pub fn check_grant_permission(access_vec: KeyPermSet, key: &KeyDescriptor) -> anyhow::Result<()> { |
| 58 | ThreadState::with_calling_sid(|calling_sid| { |
| 59 | permission::check_grant_permission( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 60 | calling_sid.ok_or_else(Error::sys).context( |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 61 | "In check_grant_permission: Cannot check permission without calling_sid.", |
| 62 | )?, |
| 63 | access_vec, |
| 64 | key, |
| 65 | ) |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | /// This function uses its namesake in the permission module and in |
| 70 | /// combination with with_calling_sid from the binder crate to check |
| 71 | /// if the caller has the given key permission. |
| 72 | pub fn check_key_permission( |
| 73 | perm: KeyPerm, |
| 74 | key: &KeyDescriptor, |
| 75 | access_vector: &Option<KeyPermSet>, |
| 76 | ) -> anyhow::Result<()> { |
| 77 | ThreadState::with_calling_sid(|calling_sid| { |
| 78 | permission::check_key_permission( |
Janis Danisevskis | 4576002 | 2021-01-19 16:34:10 -0800 | [diff] [blame] | 79 | ThreadState::get_calling_uid(), |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 80 | calling_sid |
Janis Danisevskis | a75e208 | 2020-10-07 16:44:26 -0700 | [diff] [blame] | 81 | .ok_or_else(Error::sys) |
| 82 | .context("In check_key_permission: Cannot check permission without calling_sid.")?, |
| 83 | perm, |
| 84 | key, |
| 85 | access_vector, |
| 86 | ) |
| 87 | }) |
| 88 | } |
| 89 | |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 90 | /// This function checks whether a given tag corresponds to the access of device identifiers. |
| 91 | pub fn is_device_id_attestation_tag(tag: Tag) -> bool { |
Janis Danisevskis | 83116e5 | 2021-04-06 13:36:58 -0700 | [diff] [blame] | 92 | matches!( |
| 93 | tag, |
| 94 | Tag::ATTESTATION_ID_IMEI |
| 95 | | Tag::ATTESTATION_ID_MEID |
| 96 | | Tag::ATTESTATION_ID_SERIAL |
| 97 | | Tag::DEVICE_UNIQUE_ATTESTATION |
| 98 | ) |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /// This function checks whether the calling app has the Android permissions needed to attest device |
| 102 | /// identifiers. It throws an error if the permissions cannot be verified, or if the caller doesn't |
| 103 | /// have the right permissions, and returns silently otherwise. |
| 104 | pub fn check_device_attestation_permissions() -> anyhow::Result<()> { |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 105 | let permission_controller: Strong<dyn IPermissionController::IPermissionController> = |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 106 | binder::get_interface("permission")?; |
| 107 | |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 108 | let binder_result = { |
| 109 | let _wp = watchdog::watch_millis( |
| 110 | "In check_device_attestation_permissions: calling checkPermission.", |
| 111 | 500, |
| 112 | ); |
| 113 | permission_controller.checkPermission( |
| 114 | "android.permission.READ_PRIVILEGED_PHONE_STATE", |
| 115 | ThreadState::get_calling_pid(), |
| 116 | ThreadState::get_calling_uid() as i32, |
| 117 | ) |
| 118 | }; |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 119 | let has_permissions = map_binder_status(binder_result) |
| 120 | .context("In check_device_attestation_permissions: checkPermission failed")?; |
| 121 | match has_permissions { |
| 122 | true => Ok(()), |
| 123 | false => Err(Error::Km(ErrorCode::CANNOT_ATTEST_IDS)).context(concat!( |
| 124 | "In check_device_attestation_permissions: ", |
| 125 | "caller does not have the permission to attest device IDs" |
| 126 | )), |
| 127 | } |
| 128 | } |
| 129 | |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 130 | /// Converts a set of key characteristics as returned from KeyMint into the internal |
| 131 | /// representation of the keystore service. |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 132 | pub fn key_characteristics_to_internal( |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 133 | key_characteristics: Vec<KeyCharacteristics>, |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 134 | ) -> Vec<crate::key_parameter::KeyParameter> { |
| 135 | key_characteristics |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 136 | .into_iter() |
Shawn Willden | dbdac60 | 2021-01-12 22:35:16 -0700 | [diff] [blame] | 137 | .flat_map(|aidl_key_char| { |
| 138 | let sec_level = aidl_key_char.securityLevel; |
| 139 | aidl_key_char.authorizations.into_iter().map(move |aidl_kp| { |
| 140 | crate::key_parameter::KeyParameter::new(aidl_kp.into(), sec_level) |
| 141 | }) |
| 142 | }) |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 143 | .collect() |
| 144 | } |
| 145 | |
| 146 | /// Converts a set of key characteristics from the internal representation into a set of |
| 147 | /// Authorizations as they are used to convey key characteristics to the clients of keystore. |
| 148 | pub fn key_parameters_to_authorizations( |
| 149 | parameters: Vec<crate::key_parameter::KeyParameter>, |
| 150 | ) -> Vec<Authorization> { |
| 151 | parameters.into_iter().map(|p| p.into_authorization()).collect() |
| 152 | } |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 153 | |
Hasini Gunasinghe | 66a2460 | 2021-05-12 19:03:12 +0000 | [diff] [blame] | 154 | /// This returns the current time (in milliseconds) as an instance of a monotonic clock, |
| 155 | /// by invoking the system call since Rust does not support getting monotonic time instance |
| 156 | /// as an integer. |
| 157 | pub fn get_current_time_in_milliseconds() -> i64 { |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 158 | let mut current_time = libc::timespec { tv_sec: 0, tv_nsec: 0 }; |
| 159 | // Following unsafe block includes one system call to get monotonic time. |
| 160 | // Therefore, it is not considered harmful. |
| 161 | unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC_RAW, &mut current_time) }; |
Hasini Gunasinghe | 66a2460 | 2021-05-12 19:03:12 +0000 | [diff] [blame] | 162 | 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] | 163 | } |
Janis Danisevskis | cd1fb3a | 2020-12-01 09:20:09 -0800 | [diff] [blame] | 164 | |
Janis Danisevskis | 7a1cf38 | 2020-11-20 11:22:14 -0800 | [diff] [blame] | 165 | /// Converts a response code as returned by the Android Protected Confirmation HIDL compatibility |
| 166 | /// module (keystore2_apc_compat) into a ResponseCode as defined by the APC AIDL |
| 167 | /// (android.security.apc) spec. |
| 168 | pub fn compat_2_response_code(rc: u32) -> ApcResponseCode { |
| 169 | match rc { |
| 170 | APC_COMPAT_ERROR_OK => ApcResponseCode::OK, |
| 171 | APC_COMPAT_ERROR_CANCELLED => ApcResponseCode::CANCELLED, |
| 172 | APC_COMPAT_ERROR_ABORTED => ApcResponseCode::ABORTED, |
| 173 | APC_COMPAT_ERROR_OPERATION_PENDING => ApcResponseCode::OPERATION_PENDING, |
| 174 | APC_COMPAT_ERROR_IGNORED => ApcResponseCode::IGNORED, |
| 175 | APC_COMPAT_ERROR_SYSTEM_ERROR => ApcResponseCode::SYSTEM_ERROR, |
| 176 | _ => ApcResponseCode::SYSTEM_ERROR, |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /// Converts the UI Options flags as defined by the APC AIDL (android.security.apc) spec into |
| 181 | /// UI Options flags as defined by the Android Protected Confirmation HIDL compatibility |
| 182 | /// module (keystore2_apc_compat). |
| 183 | pub fn ui_opts_2_compat(opt: i32) -> ApcCompatUiOptions { |
| 184 | ApcCompatUiOptions { |
| 185 | inverted: (opt & FLAG_UI_OPTION_INVERTED) != 0, |
| 186 | magnified: (opt & FLAG_UI_OPTION_MAGNIFIED) != 0, |
| 187 | } |
| 188 | } |
| 189 | |
Janis Danisevskis | cd1fb3a | 2020-12-01 09:20:09 -0800 | [diff] [blame] | 190 | /// AID offset for uid space partitioning. |
Joel Galenson | 81a50f2 | 2021-07-29 15:39:10 -0700 | [diff] [blame^] | 191 | pub const AID_USER_OFFSET: u32 = rustutils::users::AID_USER_OFFSET; |
Janis Danisevskis | cd1fb3a | 2020-12-01 09:20:09 -0800 | [diff] [blame] | 192 | |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 193 | /// AID of the keystore process itself, used for keys that |
| 194 | /// keystore generates for its own use. |
Joel Galenson | 81a50f2 | 2021-07-29 15:39:10 -0700 | [diff] [blame^] | 195 | pub const AID_KEYSTORE: u32 = rustutils::users::AID_KEYSTORE; |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 196 | |
Janis Danisevskis | cd1fb3a | 2020-12-01 09:20:09 -0800 | [diff] [blame] | 197 | /// Extracts the android user from the given uid. |
| 198 | pub fn uid_to_android_user(uid: u32) -> u32 { |
Joel Galenson | 81a50f2 | 2021-07-29 15:39:10 -0700 | [diff] [blame^] | 199 | rustutils::users::multiuser_get_user_id(uid) |
Janis Danisevskis | cd1fb3a | 2020-12-01 09:20:09 -0800 | [diff] [blame] | 200 | } |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 201 | |
Janis Danisevskis | 3d5a214 | 2021-05-05 07:31:24 -0700 | [diff] [blame] | 202 | /// This module provides helpers for simplified use of the watchdog module. |
| 203 | #[cfg(feature = "watchdog")] |
| 204 | pub mod watchdog { |
| 205 | pub use crate::watchdog::WatchPoint; |
| 206 | use crate::watchdog::Watchdog; |
| 207 | use lazy_static::lazy_static; |
| 208 | use std::sync::Arc; |
| 209 | use std::time::Duration; |
| 210 | |
| 211 | lazy_static! { |
| 212 | /// A Watchdog thread, that can be used to create watch points. |
| 213 | static ref WD: Arc<Watchdog> = Watchdog::new(Duration::from_secs(10)); |
| 214 | } |
| 215 | |
| 216 | /// Sets a watch point with `id` and a timeout of `millis` milliseconds. |
| 217 | pub fn watch_millis(id: &'static str, millis: u64) -> Option<WatchPoint> { |
| 218 | Watchdog::watch(&WD, id, Duration::from_millis(millis)) |
| 219 | } |
| 220 | |
| 221 | /// Like `watch_millis` but with a callback that is called every time a report |
| 222 | /// is printed about this watch point. |
| 223 | pub fn watch_millis_with( |
| 224 | id: &'static str, |
| 225 | millis: u64, |
| 226 | callback: impl Fn() -> String + Send + 'static, |
| 227 | ) -> Option<WatchPoint> { |
| 228 | Watchdog::watch_with(&WD, id, Duration::from_millis(millis), callback) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /// This module provides empty/noop implementations of the watch dog utility functions. |
| 233 | #[cfg(not(feature = "watchdog"))] |
| 234 | pub mod watchdog { |
| 235 | /// Noop watch point. |
| 236 | pub struct WatchPoint(); |
| 237 | /// Sets a Noop watch point. |
| 238 | fn watch_millis(_: &'static str, _: u64) -> Option<WatchPoint> { |
| 239 | None |
| 240 | } |
| 241 | |
| 242 | pub fn watch_millis_with( |
| 243 | _: &'static str, |
| 244 | _: u64, |
| 245 | _: impl Fn() -> String + Send + 'static, |
| 246 | ) -> Option<WatchPoint> { |
| 247 | None |
| 248 | } |
| 249 | } |
| 250 | |
Bram Bonné | 5d6c510 | 2021-02-24 15:09:18 +0100 | [diff] [blame] | 251 | #[cfg(test)] |
| 252 | mod tests { |
| 253 | use super::*; |
| 254 | use anyhow::Result; |
| 255 | |
| 256 | #[test] |
| 257 | fn check_device_attestation_permissions_test() -> Result<()> { |
| 258 | check_device_attestation_permissions().or_else(|error| { |
| 259 | match error.root_cause().downcast_ref::<Error>() { |
| 260 | // Expected: the context for this test might not be allowed to attest device IDs. |
| 261 | Some(Error::Km(ErrorCode::CANNOT_ATTEST_IDS)) => Ok(()), |
| 262 | // Other errors are unexpected |
| 263 | _ => Err(error), |
| 264 | } |
| 265 | }) |
| 266 | } |
| 267 | } |