Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [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 | |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 15 | //! This module implements IKeystoreAuthorization AIDL interface. |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 16 | |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 17 | use crate::error::Error as KeystoreError; |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 18 | use crate::globals::{ENFORCEMENTS, SUPER_KEY, DB, LEGACY_MIGRATOR}; |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 19 | use crate::permission::KeystorePerm; |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 20 | use crate::super_key::UserState; |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 21 | use crate::utils::check_keystore_permission; |
| 22 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 23 | HardwareAuthToken::HardwareAuthToken, |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 24 | }; |
Hasini Gunasinghe | b3715fb | 2021-02-26 20:34:45 +0000 | [diff] [blame^] | 25 | use android_security_authorization::binder::{ExceptionCode, Interface, Result as BinderResult, |
| 26 | Strong, Status as BinderStatus}; |
| 27 | use android_security_authorization::aidl::android::security::authorization::{ |
| 28 | IKeystoreAuthorization::BnKeystoreAuthorization, IKeystoreAuthorization::IKeystoreAuthorization, |
| 29 | LockScreenEvent::LockScreenEvent, AuthorizationTokens::AuthorizationTokens, |
| 30 | ResponseCode::ResponseCode, |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 31 | }; |
Hasini Gunasinghe | b3715fb | 2021-02-26 20:34:45 +0000 | [diff] [blame^] | 32 | use android_system_keystore2::aidl::android::system::keystore2::{ |
| 33 | ResponseCode::ResponseCode as KsResponseCode }; |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 34 | use anyhow::{Context, Result}; |
| 35 | use binder::IBinder; |
Hasini Gunasinghe | b3715fb | 2021-02-26 20:34:45 +0000 | [diff] [blame^] | 36 | use keystore2_selinux as selinux; |
| 37 | |
| 38 | /// This is the Authorization error type, it wraps binder exceptions and the |
| 39 | /// Authorization ResponseCode |
| 40 | #[derive(Debug, thiserror::Error, PartialEq)] |
| 41 | pub enum Error { |
| 42 | /// Wraps an IKeystoreAuthorization response code as defined by |
| 43 | /// android.security.authorization AIDL interface specification. |
| 44 | #[error("Error::Rc({0:?})")] |
| 45 | Rc(ResponseCode), |
| 46 | /// Wraps a Binder exception code other than a service specific exception. |
| 47 | #[error("Binder exception code {0:?}, {1:?}")] |
| 48 | Binder(ExceptionCode, i32), |
| 49 | } |
| 50 | |
| 51 | /// This function should be used by authorization service calls to translate error conditions |
| 52 | /// into service specific exceptions. |
| 53 | /// |
| 54 | /// All error conditions get logged by this function. |
| 55 | /// |
| 56 | /// `Error::Rc(x)` variants get mapped onto a service specific error code of `x`. |
| 57 | /// Certain response codes may be returned from keystore/ResponseCode.aidl by the keystore2 modules, |
| 58 | /// which are then converted to the corresponding response codes of android.security.authorization |
| 59 | /// AIDL interface specification. |
| 60 | /// |
| 61 | /// `selinux::Error::perm()` is mapped on `ResponseCode::PERMISSION_DENIED`. |
| 62 | /// |
| 63 | /// All non `Error` error conditions get mapped onto ResponseCode::SYSTEM_ERROR`. |
| 64 | /// |
| 65 | /// `handle_ok` will be called if `result` is `Ok(value)` where `value` will be passed |
| 66 | /// as argument to `handle_ok`. `handle_ok` must generate a `BinderResult<T>`, but it |
| 67 | /// typically returns Ok(value). |
| 68 | pub fn map_or_log_err<T, U, F>(result: Result<U>, handle_ok: F) -> BinderResult<T> |
| 69 | where |
| 70 | F: FnOnce(U) -> BinderResult<T>, |
| 71 | { |
| 72 | result.map_or_else( |
| 73 | |e| { |
| 74 | log::error!("{:#?}", e); |
| 75 | let root_cause = e.root_cause(); |
| 76 | if let Some(KeystoreError::Rc(ks_rcode)) = root_cause.downcast_ref::<KeystoreError>() { |
| 77 | let rc = match *ks_rcode { |
| 78 | // Although currently keystore2/ResponseCode.aidl and |
| 79 | // authorization/ResponseCode.aidl share the same integer values for the |
| 80 | // common response codes, this may deviate in the future, hence the |
| 81 | // conversion here. |
| 82 | KsResponseCode::SYSTEM_ERROR => ResponseCode::SYSTEM_ERROR.0, |
| 83 | KsResponseCode::KEY_NOT_FOUND => ResponseCode::KEY_NOT_FOUND.0, |
| 84 | KsResponseCode::VALUE_CORRUPTED => ResponseCode::VALUE_CORRUPTED.0, |
| 85 | KsResponseCode::INVALID_ARGUMENT => ResponseCode::INVALID_ARGUMENT.0, |
| 86 | // If the code paths of IKeystoreAuthorization aidl's methods happen to return |
| 87 | // other error codes from KsResponseCode in the future, they should be converted |
| 88 | // as well. |
| 89 | _ => ResponseCode::SYSTEM_ERROR.0, |
| 90 | }; |
| 91 | return Err(BinderStatus::new_service_specific_error(rc, None)); |
| 92 | } |
| 93 | let rc = match root_cause.downcast_ref::<Error>() { |
| 94 | Some(Error::Rc(rcode)) => rcode.0, |
| 95 | Some(Error::Binder(_, _)) => ResponseCode::SYSTEM_ERROR.0, |
| 96 | None => match root_cause.downcast_ref::<selinux::Error>() { |
| 97 | Some(selinux::Error::PermissionDenied) => ResponseCode::PERMISSION_DENIED.0, |
| 98 | _ => ResponseCode::SYSTEM_ERROR.0, |
| 99 | }, |
| 100 | }; |
| 101 | Err(BinderStatus::new_service_specific_error(rc, None)) |
| 102 | }, |
| 103 | handle_ok, |
| 104 | ) |
| 105 | } |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 106 | |
| 107 | /// This struct is defined to implement the aforementioned AIDL interface. |
| 108 | /// As of now, it is an empty struct. |
| 109 | pub struct AuthorizationManager; |
| 110 | |
| 111 | impl AuthorizationManager { |
| 112 | /// Create a new instance of Keystore Authorization service. |
Stephen Crane | 221bbb5 | 2020-12-16 15:52:10 -0800 | [diff] [blame] | 113 | pub fn new_native_binder() -> Result<Strong<dyn IKeystoreAuthorization>> { |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 114 | let result = BnKeystoreAuthorization::new_binder(Self); |
| 115 | result.as_binder().set_requesting_sid(true); |
| 116 | Ok(result) |
| 117 | } |
| 118 | |
| 119 | fn add_auth_token(&self, auth_token: &HardwareAuthToken) -> Result<()> { |
| 120 | //check keystore permission |
| 121 | check_keystore_permission(KeystorePerm::add_auth()).context("In add_auth_token.")?; |
| 122 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 123 | ENFORCEMENTS.add_auth_token(auth_token.clone())?; |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 124 | Ok(()) |
| 125 | } |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 126 | |
| 127 | fn on_lock_screen_event( |
| 128 | &self, |
| 129 | lock_screen_event: LockScreenEvent, |
| 130 | user_id: i32, |
| 131 | password: Option<&[u8]>, |
| 132 | ) -> Result<()> { |
| 133 | match (lock_screen_event, password) { |
| 134 | (LockScreenEvent::UNLOCK, Some(user_password)) => { |
| 135 | //This corresponds to the unlock() method in legacy keystore API. |
| 136 | //check permission |
| 137 | check_keystore_permission(KeystorePerm::unlock()) |
| 138 | .context("In on_lock_screen_event: Unlock with password.")?; |
| 139 | ENFORCEMENTS.set_device_locked(user_id, false); |
| 140 | // Unlock super key. |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 141 | if let UserState::Uninitialized = DB |
| 142 | .with(|db| { |
| 143 | UserState::get_with_password_unlock( |
| 144 | &mut db.borrow_mut(), |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 145 | &LEGACY_MIGRATOR, |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 146 | &SUPER_KEY, |
| 147 | user_id as u32, |
| 148 | user_password, |
| 149 | ) |
| 150 | }) |
| 151 | .context("In on_lock_screen_event: Unlock with password.")? |
| 152 | { |
| 153 | log::info!( |
| 154 | "In on_lock_screen_event. Trying to unlock when LSKF is uninitialized." |
| 155 | ); |
| 156 | } |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 157 | |
| 158 | Ok(()) |
| 159 | } |
| 160 | (LockScreenEvent::UNLOCK, None) => { |
| 161 | check_keystore_permission(KeystorePerm::unlock()) |
| 162 | .context("In on_lock_screen_event: Unlock.")?; |
| 163 | ENFORCEMENTS.set_device_locked(user_id, false); |
| 164 | Ok(()) |
| 165 | } |
| 166 | (LockScreenEvent::LOCK, None) => { |
| 167 | check_keystore_permission(KeystorePerm::lock()) |
| 168 | .context("In on_lock_screen_event: Lock")?; |
| 169 | ENFORCEMENTS.set_device_locked(user_id, true); |
| 170 | Ok(()) |
| 171 | } |
| 172 | _ => { |
| 173 | // Any other combination is not supported. |
Hasini Gunasinghe | b3715fb | 2021-02-26 20:34:45 +0000 | [diff] [blame^] | 174 | Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)) |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 175 | .context("In on_lock_screen_event: Unknown event.") |
| 176 | } |
| 177 | } |
| 178 | } |
Hasini Gunasinghe | b3715fb | 2021-02-26 20:34:45 +0000 | [diff] [blame^] | 179 | |
| 180 | fn get_auth_tokens_for_credstore( |
| 181 | &self, |
| 182 | challenge: i64, |
| 183 | secure_user_id: i64, |
| 184 | auth_token_max_age_millis: i64, |
| 185 | ) -> Result<AuthorizationTokens> { |
| 186 | // Check permission. Function should return if this failed. Therefore having '?' at the end |
| 187 | // is very important. |
| 188 | check_keystore_permission(KeystorePerm::get_auth_token()) |
| 189 | .context("In get_auth_tokens_for_credstore.")?; |
| 190 | |
| 191 | // if the challenge is zero, return error |
| 192 | if challenge == 0 { |
| 193 | return Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)) |
| 194 | .context("In get_auth_tokens_for_credstore. Challenge can not be zero."); |
| 195 | } |
| 196 | // Obtain the auth token and the timestamp token from the enforcement module. |
| 197 | let (auth_token, ts_token) = |
| 198 | ENFORCEMENTS.get_auth_tokens(challenge, secure_user_id, auth_token_max_age_millis)?; |
| 199 | Ok(AuthorizationTokens { authToken: auth_token, timestampToken: ts_token }) |
| 200 | } |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | impl Interface for AuthorizationManager {} |
| 204 | |
| 205 | impl IKeystoreAuthorization for AuthorizationManager { |
| 206 | fn addAuthToken(&self, auth_token: &HardwareAuthToken) -> BinderResult<()> { |
| 207 | map_or_log_err(self.add_auth_token(auth_token), Ok) |
| 208 | } |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 209 | |
| 210 | fn onLockScreenEvent( |
| 211 | &self, |
| 212 | lock_screen_event: LockScreenEvent, |
| 213 | user_id: i32, |
| 214 | password: Option<&[u8]>, |
| 215 | ) -> BinderResult<()> { |
| 216 | map_or_log_err(self.on_lock_screen_event(lock_screen_event, user_id, password), Ok) |
| 217 | } |
Hasini Gunasinghe | b3715fb | 2021-02-26 20:34:45 +0000 | [diff] [blame^] | 218 | |
| 219 | fn getAuthTokensForCredStore( |
| 220 | &self, |
| 221 | challenge: i64, |
| 222 | secure_user_id: i64, |
| 223 | auth_token_max_age_millis: i64, |
| 224 | ) -> binder::public_api::Result<AuthorizationTokens> { |
| 225 | map_or_log_err( |
| 226 | self.get_auth_tokens_for_credstore( |
| 227 | challenge, |
| 228 | secure_user_id, |
| 229 | auth_token_max_age_millis, |
| 230 | ), |
| 231 | Ok, |
| 232 | ) |
| 233 | } |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 234 | } |