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 | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame^] | 19 | use crate::globals::{ENFORCEMENTS, SUPER_KEY, DB}; |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 20 | use crate::permission::KeystorePerm; |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame^] | 21 | use crate::super_key::UserState; |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 22 | use crate::utils::check_keystore_permission; |
| 23 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 24 | HardwareAuthToken::HardwareAuthToken, |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 25 | }; |
Stephen Crane | 221bbb5 | 2020-12-16 15:52:10 -0800 | [diff] [blame] | 26 | use android_security_authorization::binder::{Interface, Result as BinderResult, Strong}; |
| 27 | use android_security_authorization::aidl::android::security::authorization::IKeystoreAuthorization::{ |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 28 | BnKeystoreAuthorization, IKeystoreAuthorization, |
| 29 | }; |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 30 | use android_security_authorization:: aidl::android::security::authorization::LockScreenEvent::LockScreenEvent; |
| 31 | use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode; |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 32 | use anyhow::{Context, Result}; |
| 33 | use binder::IBinder; |
| 34 | |
| 35 | /// This struct is defined to implement the aforementioned AIDL interface. |
| 36 | /// As of now, it is an empty struct. |
| 37 | pub struct AuthorizationManager; |
| 38 | |
| 39 | impl AuthorizationManager { |
| 40 | /// Create a new instance of Keystore Authorization service. |
Stephen Crane | 221bbb5 | 2020-12-16 15:52:10 -0800 | [diff] [blame] | 41 | pub fn new_native_binder() -> Result<Strong<dyn IKeystoreAuthorization>> { |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 42 | let result = BnKeystoreAuthorization::new_binder(Self); |
| 43 | result.as_binder().set_requesting_sid(true); |
| 44 | Ok(result) |
| 45 | } |
| 46 | |
| 47 | fn add_auth_token(&self, auth_token: &HardwareAuthToken) -> Result<()> { |
| 48 | //check keystore permission |
| 49 | check_keystore_permission(KeystorePerm::add_auth()).context("In add_auth_token.")?; |
| 50 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 51 | ENFORCEMENTS.add_auth_token(auth_token.clone())?; |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 52 | Ok(()) |
| 53 | } |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 54 | |
| 55 | fn on_lock_screen_event( |
| 56 | &self, |
| 57 | lock_screen_event: LockScreenEvent, |
| 58 | user_id: i32, |
| 59 | password: Option<&[u8]>, |
| 60 | ) -> Result<()> { |
| 61 | match (lock_screen_event, password) { |
| 62 | (LockScreenEvent::UNLOCK, Some(user_password)) => { |
| 63 | //This corresponds to the unlock() method in legacy keystore API. |
| 64 | //check permission |
| 65 | check_keystore_permission(KeystorePerm::unlock()) |
| 66 | .context("In on_lock_screen_event: Unlock with password.")?; |
| 67 | ENFORCEMENTS.set_device_locked(user_id, false); |
| 68 | // Unlock super key. |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame^] | 69 | if let UserState::Uninitialized = DB |
| 70 | .with(|db| { |
| 71 | UserState::get_with_password_unlock( |
| 72 | &mut db.borrow_mut(), |
| 73 | &SUPER_KEY, |
| 74 | user_id as u32, |
| 75 | user_password, |
| 76 | ) |
| 77 | }) |
| 78 | .context("In on_lock_screen_event: Unlock with password.")? |
| 79 | { |
| 80 | log::info!( |
| 81 | "In on_lock_screen_event. Trying to unlock when LSKF is uninitialized." |
| 82 | ); |
| 83 | } |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 84 | |
| 85 | Ok(()) |
| 86 | } |
| 87 | (LockScreenEvent::UNLOCK, None) => { |
| 88 | check_keystore_permission(KeystorePerm::unlock()) |
| 89 | .context("In on_lock_screen_event: Unlock.")?; |
| 90 | ENFORCEMENTS.set_device_locked(user_id, false); |
| 91 | Ok(()) |
| 92 | } |
| 93 | (LockScreenEvent::LOCK, None) => { |
| 94 | check_keystore_permission(KeystorePerm::lock()) |
| 95 | .context("In on_lock_screen_event: Lock")?; |
| 96 | ENFORCEMENTS.set_device_locked(user_id, true); |
| 97 | Ok(()) |
| 98 | } |
| 99 | _ => { |
| 100 | // Any other combination is not supported. |
| 101 | Err(KeystoreError::Rc(ResponseCode::INVALID_ARGUMENT)) |
| 102 | .context("In on_lock_screen_event: Unknown event.") |
| 103 | } |
| 104 | } |
| 105 | } |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | impl Interface for AuthorizationManager {} |
| 109 | |
| 110 | impl IKeystoreAuthorization for AuthorizationManager { |
| 111 | fn addAuthToken(&self, auth_token: &HardwareAuthToken) -> BinderResult<()> { |
| 112 | map_or_log_err(self.add_auth_token(auth_token), Ok) |
| 113 | } |
Hasini Gunasinghe | a020b53 | 2021-01-07 21:42:35 +0000 | [diff] [blame] | 114 | |
| 115 | fn onLockScreenEvent( |
| 116 | &self, |
| 117 | lock_screen_event: LockScreenEvent, |
| 118 | user_id: i32, |
| 119 | password: Option<&[u8]>, |
| 120 | ) -> BinderResult<()> { |
| 121 | map_or_log_err(self.on_lock_screen_event(lock_screen_event, user_id, password), Ok) |
| 122 | } |
Janis Danisevskis | 9f10a6a | 2021-01-18 16:45:21 +0000 | [diff] [blame] | 123 | } |