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; |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 18 | use crate::error::map_or_log_err; |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 19 | use crate::globals::{DB, ENFORCEMENTS, LEGACY_BLOB_LOADER, SUPER_KEY}; |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 20 | use crate::permission::KeystorePerm; |
| 21 | use crate::utils::check_keystore_permission; |
| 22 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
| 23 | HardwareAuthToken::HardwareAuthToken, HardwareAuthenticatorType::HardwareAuthenticatorType, |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 24 | }; |
| 25 | use android_hardware_security_secureclock::aidl::android::hardware::security::secureclock::{ |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 26 | Timestamp::Timestamp, |
| 27 | }; |
Stephen Crane | 221bbb5 | 2020-12-16 15:52:10 -0800 | [diff] [blame] | 28 | use android_security_authorization::binder::{Interface, Result as BinderResult, Strong}; |
| 29 | use android_security_authorization::aidl::android::security::authorization::IKeystoreAuthorization::{ |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 30 | BnKeystoreAuthorization, IKeystoreAuthorization, |
| 31 | }; |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 32 | use android_security_authorization:: aidl::android::security::authorization::LockScreenEvent::LockScreenEvent; |
| 33 | use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode; |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 34 | use anyhow::{Context, Result}; |
| 35 | use binder::IBinder; |
| 36 | |
| 37 | /// This struct is defined to implement the aforementioned AIDL interface. |
| 38 | /// As of now, it is an empty struct. |
| 39 | pub struct AuthorizationManager; |
| 40 | |
| 41 | impl AuthorizationManager { |
| 42 | /// Create a new instance of Keystore Authorization service. |
Stephen Crane | 221bbb5 | 2020-12-16 15:52:10 -0800 | [diff] [blame] | 43 | pub fn new_native_binder() -> Result<Strong<dyn IKeystoreAuthorization>> { |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 44 | let result = BnKeystoreAuthorization::new_binder(Self); |
| 45 | result.as_binder().set_requesting_sid(true); |
| 46 | Ok(result) |
| 47 | } |
| 48 | |
| 49 | fn add_auth_token(&self, auth_token: &HardwareAuthToken) -> Result<()> { |
| 50 | //check keystore permission |
| 51 | check_keystore_permission(KeystorePerm::add_auth()).context("In add_auth_token.")?; |
| 52 | |
| 53 | //TODO: Keymint's HardwareAuthToken aidl needs to implement Copy/Clone |
| 54 | let auth_token_copy = HardwareAuthToken { |
| 55 | challenge: auth_token.challenge, |
| 56 | userId: auth_token.userId, |
| 57 | authenticatorId: auth_token.authenticatorId, |
| 58 | authenticatorType: HardwareAuthenticatorType(auth_token.authenticatorType.0), |
| 59 | timestamp: Timestamp { milliSeconds: auth_token.timestamp.milliSeconds }, |
| 60 | mac: auth_token.mac.clone(), |
| 61 | }; |
| 62 | ENFORCEMENTS.add_auth_token(auth_token_copy)?; |
| 63 | Ok(()) |
| 64 | } |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 65 | |
| 66 | fn on_lock_screen_event( |
| 67 | &self, |
| 68 | lock_screen_event: LockScreenEvent, |
| 69 | user_id: i32, |
| 70 | password: Option<&[u8]>, |
| 71 | ) -> Result<()> { |
| 72 | match (lock_screen_event, password) { |
| 73 | (LockScreenEvent::UNLOCK, Some(user_password)) => { |
| 74 | //This corresponds to the unlock() method in legacy keystore API. |
| 75 | //check permission |
| 76 | check_keystore_permission(KeystorePerm::unlock()) |
| 77 | .context("In on_lock_screen_event: Unlock with password.")?; |
| 78 | ENFORCEMENTS.set_device_locked(user_id, false); |
| 79 | // Unlock super key. |
| 80 | DB.with::<_, Result<()>>(|db| { |
| 81 | let mut db = db.borrow_mut(); |
| 82 | //TODO - b/176123105 - Once the user management API is implemented, unlock is |
| 83 | //allowed only if the user is added. Then the two tasks handled by the |
| 84 | //unlock_user_key will be split into two methods. For now, unlock_user_key |
| 85 | //method is used as it is, which created a super key for the user if one does |
| 86 | //not exists, in addition to unlocking the existing super key of the user/ |
| 87 | SUPER_KEY.unlock_user_key( |
| 88 | user_id as u32, |
| 89 | user_password, |
| 90 | &mut db, |
| 91 | &LEGACY_BLOB_LOADER, |
| 92 | )?; |
| 93 | Ok(()) |
| 94 | }) |
| 95 | .context("In on_lock_screen_event.")?; |
| 96 | |
| 97 | Ok(()) |
| 98 | } |
| 99 | (LockScreenEvent::UNLOCK, None) => { |
| 100 | check_keystore_permission(KeystorePerm::unlock()) |
| 101 | .context("In on_lock_screen_event: Unlock.")?; |
| 102 | ENFORCEMENTS.set_device_locked(user_id, false); |
| 103 | Ok(()) |
| 104 | } |
| 105 | (LockScreenEvent::LOCK, None) => { |
| 106 | check_keystore_permission(KeystorePerm::lock()) |
| 107 | .context("In on_lock_screen_event: Lock")?; |
| 108 | ENFORCEMENTS.set_device_locked(user_id, true); |
| 109 | Ok(()) |
| 110 | } |
| 111 | _ => { |
| 112 | // Any other combination is not supported. |
| 113 | Err(KeystoreError::Rc(ResponseCode::INVALID_ARGUMENT)) |
| 114 | .context("In on_lock_screen_event: Unknown event.") |
| 115 | } |
| 116 | } |
| 117 | } |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | impl Interface for AuthorizationManager {} |
| 121 | |
| 122 | impl IKeystoreAuthorization for AuthorizationManager { |
| 123 | fn addAuthToken(&self, auth_token: &HardwareAuthToken) -> BinderResult<()> { |
| 124 | map_or_log_err(self.add_auth_token(auth_token), Ok) |
| 125 | } |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 126 | |
| 127 | fn onLockScreenEvent( |
| 128 | &self, |
| 129 | lock_screen_event: LockScreenEvent, |
| 130 | user_id: i32, |
| 131 | password: Option<&[u8]>, |
| 132 | ) -> BinderResult<()> { |
| 133 | map_or_log_err(self.on_lock_screen_event(lock_screen_event, user_id, password), Ok) |
| 134 | } |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 135 | } |