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